• 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
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

regular expression to find a pattern of string but not following another pattern of string

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Example, I have a string
openshift.cluster_id:20bd0f2c-0d96-4c9a-b730-b4db9de53ce4

I want to use regular expression to find another string start with "openshift.cluster_id:" but not follow the pattern "20bd0f2c-0d96-4c9a-b730-b4db9de53ce4", how to do this?

 
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have an example of a string that you want to match, and an example of a string that should not match?
 
peter tong
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

peter tong wrote:Example, I have a string
openshift.cluster_id:20bd0f2c-0d96-4c9a-b730-b4db9de53ce4

I want to use regular expression to find another string start with "openshift.cluster_id:" but not follow the pattern "20bd0f2c-0d96-4c9a-b730-b4db9de53ce4", how to do this?



I found this regular expression, using Negative lookahead, fulfill my criteria:


openshift.cluster_id:20bd0f2c-0d96-4c9a-b730-b4db9de53ce4 not find any match
openshift.cluster_id:02bd0f2c-0d96-4c9a-b730-b4db9de53ce4 find a match
openshift.cluster_id:120bd0f2c-0d96-4c9a-b730-b4db9de53ce4 find a match

Another similar example
find a string match "abc", but then not follow "def"
use regex:


abcdef => not find any match
abcedf => find a match
abcfde => find a match
abcdefa => not find any match
abcadef => find a match

Detail can be seen in this link
https://www.regular-expressions.info/lookaround.html
about negative lookahead
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does it have to be a single regular expression?

If possible, I'd do it in two passes...one pass to find strings with "openshift.cluster_id:", and then another that of those, filters out the ones you don't want.

I HATE complicated regular expression.  They are tricky to write correctly, hard to test, and next to impossible to remember what you were doing when you come back to them six months later (or next week, in my case).  Two simple regex's are much easier to follow than one complicated one.
 
Sheriff
Posts: 28346
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In fact, does it even need to be a regular expression? It looks to me like you just want

since you already used regex to find "blabla" and "yaddayadda".
 
Rancher
Posts: 5090
82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless you're searching within a much larger area, like a file, and the "openshift.cluster_id:" occurrs multiple times, and you're looking for the ones that aren't followed by the specific cluster id 20bd0f2c-0d96-4c9a-b730-b4db9de53ce4.  The openshift.cluster_id: isn't necessarily at the beginning, and the value isn't necessarily at the end.

Negative lookahead is exactly the tool for this problem, I think.  The regex syntax isn't very complicated at all.  Unfortunately, the cluster id looks like a unsightly mess.  But that doesn't make it a complicated regex.

Unfortunately, we should note that "openshift.cluster_id:" does contain one special character: '.'  For that, the regex should actually replace "." with "\."  And if this is being done within a Java program, you need to escape again, so that each "." is replaced with "\\\.".  Annoying.

Peter, glad you found the answer!
 
Right! We're on it! Let's get to work tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic