Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RegExpression Exact phrase

Status
Not open for further replies.

requested89

IS-IT--Management
Sep 6, 2005
44
GB
Is it possible to have RegExpr only come back if the string matches exactly with the expression?

I want to find the expressions "bla|ble|bli|blu" etc but I don't want it to trigger if the words are bland, bleed, blimp, blue etc. Is there some sort of binary mode whereby it has to match exactly?

Thanks in advance,

Chris
 
Use the [tt]^[/tt] character at the beginning of your regular expression, and the [tt]$[/tt] character at the end of the regular expression. They are metacharacters that match the beginning and the end of the input string, respectively.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
>I want to find the expressions "bla|ble|bli|blu" etc but I don't want it to trigger if the words are bland, bleed, blimp, blue etc.
If you're interested in a single regexp pattern, then you should have a fixed systematic where the words associated with bla (bland,...?) or blu (blue,...?) are. Hopefully finite number of them. Otherwise, it would be quick out of control.

The general principle handling these is negative lookahead. Luckily vbs engine v5.6 support lookahead. As a demo, like this which match a word start with "bl" but exclude "blue".
[tt]
s="The sky is blue and the blood is bloody."
set re=new regexp
re.global=true
re.pattern="\bbl(?!ue).*?\b"

for each match in re.execute(s)
response.write match & "<br />"
next
[/tt]
 
If needed to improve specificity, this could limit the specific complete word (like blue excluded, and Bluebeard accepted.
[tt]
re.global=true
re.ignorecase=true
re.pattern="\bbl(?!ue[blue]\b[/blue]).*?\b"
[/tt]
 
You can also use some of the other metacharacters to limit your search:

\b matches a "word boundary"
\B matches a non-word-boundary
\s matches a whitespace character [ \f\n\r\t\v]
\S matches a non-whitespace [^ \f\n\r\t\v]
\w matches a word character [a-zA-Z0-9_]
\W matches a non-word-character [^a-zA-Z0-9_]

For example [tt]"\w(bla|ble|bli|blu)\w"[/tt] would limit the search in the way you describe.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top