• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

regex grouping and OR

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about a*23a*
reply
    Bookmark Topic Watch Topic
  • New Topic