Oliver
Oliver

Reputation: 2212

Regex conditional lookbehind character match

So I want to find the string "to" in a string, but only when it is standalone. It could be at the beginning of the string, as in "to do this", so I can't search " to ".

What I want to do is say, if there is a character behind "to", it cannot be \w. How do I do that?

Upvotes: 0

Views: 443

Answers (2)

dorsh
dorsh

Reputation: 24720

Try using \bto\b, which will match to as a stand-alone word

Here's a good explanation:

There are three different positions that qualify as word boundaries:

  • Before the first character in the string, if the first character is a word character.

  • After the last character in the string, if the last character is a word character.

  • Between two characters in the string, where one is a word character and the other is not a word character.

Simply put: \b allows you to perform a "whole words only" search using a regular expression in the form of \bword\b. A "word character" is a character that can be used to form words. All characters that are not "word characters" are "non-word characters".

Upvotes: 1

Oybek
Oybek

Reputation: 7243

Try word boudaries. It matches the beginning and the end of the searched pattern

\bto\b

This is exaclty what you want to say, i.e.

So what exactly is it that \b matches? Regular expression engines do not understand English, or any language for that matter, and so they don't know what word boundaries are. \b simply matches a location between characters that are usually parts of words (alphanumeric characters and underscore, text that would be matched by \w) and anything else (text that would be matched by \W).

Sams Teach Yourself Regular Expressions in 10 Minutes By Ben Forta

Upvotes: 2

Related Questions