aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes regex grouping and OR Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "regex grouping and OR" Watch "regex grouping and OR" New topic
Author

regex grouping and OR

Jonas Dimsa
Greenhorn

Joined: Apr 02, 2007
Posts: 9
public static void main(String[] args) {
String str = "aaa1a234562a23a";
int i = 0;
Pattern p = Pattern.compile("[(23)a]+");
Matcher m = p.matcher(str);
while(m.find()){
System.out.println(i+++") " + m.group()+ " starts: " + m.start());
}
}

how to formate regex to find (23)or a zero o more time
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 863
    
    1
Originally posted by Jonas Dimsa:
how to formate regex to find (23)or a zero o more time


presuming you don't intended to match literal parens: ((23)|a)+

[edit: Change the + to a * if you really mean "zero or more times."]
[ February 27, 2008: Message edited by: Brian Cole ]

bitguru blog
Jonas Dimsa
Greenhorn

Joined: Apr 02, 2007
Posts: 9
yes one or more times. what i need is to find occurrences of 23a or a23 or aa23 and so on. it must be "a" or "23". i was in a hurry. and sorry for my english
Jonas Dimsa
Greenhorn

Joined: Apr 02, 2007
Posts: 9
thanks. is it possible to write such expression only with []and ()?? grouping doesn't work in [(a(23)]
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 863
    
    1
Originally posted by Jonas Dimsa:
thanks. is it possible to write such expression only with []and ()?? grouping doesn't work in [(a(23)]


Does the expression I gave above, ((23)|a)+, fail your criteria?

It uses the | operator, but it pretty much has to. The square-brackets character class construct is sort of an OR operator but it only works on characters. It is the | operator that is the general OR.
[ February 28, 2008: Message edited by: Brian Cole ]
Rahul Singhai
Greenhorn

Joined: Mar 04, 2008
Posts: 19
What about a*23a*


<br />
SCJP - 93%
Warm Regards,<br />Rollicking
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regex grouping and OR