Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex to match strings not ending with a pattern?

I am trying to form a regular expression that will match strings that do NOT end a with a DOT FOLLOWED BY NUMBER.

eg.

abcd1
abcdf12
abcdf124
abcd1.0
abcd1.134
abcdf12.13
abcdf124.2
abcdf124.21

I want to match first three.
I tried modifying this post but it didn't work for me as the number may have variable length.

Can someone help?

like image 634
Vinay Avatar asked Sep 15 '25 16:09

Vinay


1 Answers

You can use something like this:

^((?!\.[\d]+)[\w.])+$

It anchors at the start and end of a line. It basically says:

Anchor at the start of the line
DO NOT match the pattern .NUMBERS
Take every letter, digit, etc, unless we hit the pattern above
Anchor at the end of the line

So, this pattern matches this (no dot then number):

This.Is.Your.Pattern or This.Is.Your.Pattern2012

However it won't match this (dot before the number):

This.Is.Your.Pattern.2012

EDIT: In response to Wiseguy's comment, you can use this:

^((?!\.[\d]+$)[\w.])+$ - which provides an anchor after the number. Therefore, it must be a dot, then only a number at the end... not that you specified that in your question..

like image 80
Simon Whitehead Avatar answered Sep 18 '25 10:09

Simon Whitehead