A regular expression using the word boundary, but the word ends with a. (Period)

want to match the word iv case insensitive

has a template

 (?i)\bi\.v\. 

but the word boundary needs to be at the end
the above pattern fails to match ivx

but if I try to add a work boundary to the end

 (?i)\bi\.v\.\b 

it fails that it doesn’t even correspond to iv as I think, \b eats literal. as. is the word break
need to \. was greedy

I want to match | sam iv sam

don't want to match sam.iv
ivsam

It's closer

 (?i)\bi\.v\.\s$ 

But he does not find iv at the end of the line

+8
regex
source share
3 answers

\b matches only an alphanumeric character and a non-alphanumeric character (or the beginning / end of a line). Therefore, it does not match after . unless an alphanumeric character follows this point.

If you intend to prevent the character without spaces from following the period, you can indicate that by using a negative forecast of expectations

 (?i)\bi\.v\.(?!\S) 

(?!\S) means "Assert that the next character is not a character without spaces."

This may seem a bit confusing - why is double negative? Why not (?=\s) , which means "Assert that the next character is a space character"? Well, there is a subtle difference: the second version requires a space character; this means that the regular expression will not match at the end of the line. The first regular expression also processes this angular register.

If you really want the word boundary to mean limited space, you also need to replace the first \b :

 (?i)(?<!\S)i\.v\.(?!\S) 

or the regex will match sam.iv , which you don't seem to want.

+16
source share

About the current regular expression:

You do not need to have \b after the dot, since the dot is not considered a word character, but, of course, the dot must be escaped:

 (?i)\bi\.v\. 

But you need \b to i to make sure it doesn't match, for example. hi

EDIT: (based on your further changes)

Try this regex:

 (?i)\bi\.v\.(?=\s|$) 
+2
source share

You seem very confused with word boundaries and greedy concepts. The best you can do is go to these addresses:

  • What is a greedy quantifier:

http://www.regular-expressions.info/repeat.html

  • what is the word boundary:

http://www.regular-expressions.info/wordboundaries.html

When you read these explanations, I am sure you will think that your problem was ridiculous.

-one
source share

All Articles