• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Regex expression grouping

 
Ranch Hand
Posts: 163
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a regular expression that looks for any number divisible by 4 surrounded by brackets.

I have the working regex, but I'm unsure why it works and mine doesn't.





My pattern doesn't work. It accepts [2014] as a valid match.

I use an even grouping of conditions: (condition1|condition2|condition3), but it fails. The working solution utilizes ((condition1|condition2)|condition3). I don't understand the difference in the logic considering it's an OR condition.

 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bill Clar wrote:
I use an even grouping of conditions: (condition1|condition2|condition3), but it fails. The working solution utilizes ((condition1|condition2)|condition3). I don't understand the difference in the logic considering it's an OR condition.



You forgot to include the "\\d*" part in the condition in your description. After all, it is inside the parens, so shouldn't it have some effect?

Henry
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well looking at the 2 regex, the bolded parts are different.

Working = "\\[(\\d*(([02468][048])|([13579][26]))|[048])\\]"
Yours = "\\[(\\d*(([02468][048])|([13579][26])|([048])))\\]"
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bill Clar wrote:...look for any number divisible by 4 surrounded by brackets.


ughh...Why in the world would you use a regex to do this?
 
Bill Clar
Ranch Hand
Posts: 163
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:

Bill Clar wrote:...look for any number divisible by 4 surrounded by brackets.


ughh...Why in the world would you use a regex to do this?



It's a challenge from a coding site. It had to be done by regex.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bill Clar wrote:It's a challenge from a coding site. It had to be done by regex.


OK, but why surrounded by brackets? Maybe it's the same answer, but it seems kind of pointless...

Winston
 
reply
    Bookmark Topic Watch Topic
  • New Topic