• 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

problem while making java regex

 
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

i have a string which starts from uid= and ends with ou=people and i want all between this, so i am using the below regex and it's working fine for me.

(?<=\\buid=\\b).*?(?=\\b,ou=people\\b)

now my requirements are changed, now the string can also be starts with o= so i updated my regex like this :

(?<=\\buid=\\b)(?<=\\bo=\\b).*?(?=\\b,ou=people\\b)

but it is not working.

 
author
Posts: 23951
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

Punit Jain wrote:
i have a string which starts from uid= and ends with ou=people and i want all between this, so i am using the below regex and it's working fine for me.

(?<=\\buid=\\b).*?(?=\\b,ou=people\\b)

now my requirements are changed, now the string can also be starts with o= so i updated my regex like this :

(?<=\\buid=\\b)(?<=\\bo=\\b).*?(?=\\b,ou=people\\b)

but it is not working.



Of course not. Adding another zero-length look behind means that both conditions (the old and the new) needs to be satisfied. Perhaps, changing the look behind to have an alternation of the two options would work?

Henry
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ups it should be OR not AND :P

(?<=\\buid=\\b)|(?<=\\bo=\\b).*?(?=\\b,ou=people\\b)
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops ! That won't work either since the first look behind won't worry at all about what follows.The OR should be inside the look behind term.
 
Henry Wong
author
Posts: 23951
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

Richard Tookey wrote:Oops ! That won't work either since the first look behind won't worry at all about what follows.The OR should be inside the look behind term.



Agreed. An alternation within a single look behind is probably more readable than an alternation of look behinds.

Henry
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yup.
would be like this :


 
Henry Wong
author
Posts: 23951
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

Punit Jain wrote:
would be like this :





Well, have you tried it? And when you did, did it work?

Hint: The alternation operator has very low precedence, so keep that in mind when using it. And yes, I am hinting that there are cases where your solution doesn't work.

Henry
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Punit Jain wrote:
would be like this :





Well, have you tried it? And when you did, did it work?



Thanks henry
yes i have tried and it worked.

Henry Wong wrote:
Hint: The alternation operator has very low precedence, so keep that in mind when using it. And yes, I am hinting that there are cases where your solution doesn't work.
Henry



can you please explain me where it can get fail? any example?




 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem I have is that you haven't actually defined what the regex must match and not match or how you are using the regex.
 
Henry Wong
author
Posts: 23951
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

Punit Jain wrote:

Henry Wong wrote:
Hint: The alternation operator has very low precedence, so keep that in mind when using it. And yes, I am hinting that there are cases where your solution doesn't work.
Henry



can you please explain me where it can get fail? any example?



Try it with a string that starts with "hello=" and ends in "ou=people". Does it match? Do you want it to match?


Richard Tookey wrote:The problem I have is that you haven't actually defined what the regex must match and not match or how you are using the regex.



Yeah, I am somewhat speculating from the original post, and the fact that the original regex (with boundary specifiers), that the OP expects a boundary on the "o=" part.

Henry
reply
    Bookmark Topic Watch Topic
  • New Topic