Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex validate with last character

Tags:

regex

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 ?

like image 894
kosnkov Avatar asked May 02 '15 15:05

kosnkov


2 Answers

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 
like image 173
user230910 Avatar answered Oct 10 '22 21:10

user230910


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

like image 21
Praveen Paulose Avatar answered Oct 10 '22 21:10

Praveen Paulose