Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regex grouping and OR

 
Jonas Dimsa
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 908
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jonas Dimsa
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks. is it possible to write such expression only with []and ()?? grouping doesn't work in [(a(23)]
 
Brian Cole
Author
Ranch Hand
Posts: 908
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about a*23a*
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic