问题描述:

Having regex patter like this: ab*d why does it return match true for input abbbde ? How to change it to check also last character ?

网友答案:

Lets analyse your regex:

a - match an "a"

b* - match any number of b's

d - match a "d"

Because * matches any number of b's.

$ matches end of line, so

ab*d$ 

should match end of line (to make sure nothing follows)

Then again \s will match any whitespace so another option is

ab*d\s 
网友答案:

You need to add a $ at the end of the pattern to ensure it is the last character ab*d$.

A $ is called a End of String Anchor in Regular Expressions. You can read more on Anchors here http://www.regular-expressions.info/anchors.html

相关阅读:
Top