Regular Expressions

  • Thread starter Thread starter Derrick
  • Start date Start date
D

Derrick

I see the "or" operator, is there an "and"? I'd like to take an input
string,
search it for "diabetes mellitus" and match strings that contain "diabetes"
and "mellitus", but not care about the order, there may be other words
between them, "mellitus" may come first, etc.

Thanks!
 
I see the "or" operator, is there an "and"? I'd like to take an input
string,
search it for "diabetes mellitus" and match strings that contain
"diabetes" and "mellitus", but not care about the order, there may be
other words between them, "mellitus" may come first, etc.

Thanks!

Try

diabetes|mellitus

as Regex.. | means or

or

[^|\s+]diabetes\s+.*?\s+mellitus[\s+|$] diabetes before mellitus

[^|\s+]mellitus\s+.*?\s+diabetes[\s+|$] other order

explanation:

[^|\s+] Line start - or one or more spaces
diabetes Now comes diabetes
\s+ One or more spaces
..*? All between to the next
\s+ -
mellitus Now comes mellitus
[\s+|$] One or more spaces - or line end


Hope this will help you!


Greetings


Matthias
 
hi Derrick
putting strings next to each other is the anding with reguler
expression"you can define that you don't mind having whatever characters
between them as long as they both exist " so here is what you should so you
should but diabetes then mellitus once the OR it with another one that
starts with mellitus then diabetes ... hope that slove your problem
 
Just as an addendum to the solution by Matthias - you may want to match even
when the words are not bounded by spaces/beginning and end of string. For
this you can use the word boundary zero-width assertion \b. This will help
match instances that are bouded by punctuation as well:

\bdiabetes\b.*\bmellitus\b|\bmellitus\b.*\bdiabetes\b (with the
IgnoreCase and Singleline Options)

Also, you could use this other technique, although the first is easier to
read:

((?<diabetes>\bdiabetes\b)|(?<mellitus>\bmellitus\b))(?(diabetes).*\bmellitu
s\b|.*\bdiabetes\b)

Note that both would probably need to have the Singleline and IgnoreCase
options.


Brian Davis
www.knowdotnet.com



Matthias Kwiedor said:
I see the "or" operator, is there an "and"? I'd like to take an input
string,
search it for "diabetes mellitus" and match strings that contain
"diabetes" and "mellitus", but not care about the order, there may be
other words between them, "mellitus" may come first, etc.

Thanks!

Try

diabetes|mellitus

as Regex.. | means or

or

[^|\s+]diabetes\s+.*?\s+mellitus[\s+|$] diabetes before mellitus

[^|\s+]mellitus\s+.*?\s+diabetes[\s+|$] other order

explanation:

[^|\s+] Line start - or one or more spaces
diabetes Now comes diabetes
\s+ One or more spaces
.*? All between to the next
\s+ -
mellitus Now comes mellitus
[\s+|$] One or more spaces - or line end


Hope this will help you!


Greetings


Matthias
 
Back
Top