Jordan
Jordan

Reputation: 37

Regex end of string wont work

I want a REGEXP to use for mysql query.
I tried this regexp and use .* to mark anything between and the $ to mark the end of the string.

Battlefield.*2$

and i get this result that is normal since "2" and "2142" are both ending in "2":

Battlefield 2
Battlefield :2
Battlefield: 2
Battlefield collection 2
Battlefield 2142
Battlefield : 2142

My target results is this:

Battlefield 2
Battlefield :2
Battlefield: 2
Battlefield collection 2

How can i get it?

Upvotes: 2

Views: 83

Answers (3)

AbhiNickz
AbhiNickz

Reputation: 1103

$string =~ m/Battlefield[ ]?[A-Za-z]*[ ]?[:]?[ ]?2$/)

Upvotes: 0

pavel
pavel

Reputation: 27092

If the numbers aren't allowed, it should be:

Battlefield[^\d]+2$

Upvotes: 3

Denys Séguret
Denys Séguret

Reputation: 382160

If you don't want any other digit before the 2, then you can use

Battlefield.*\D2$

Upvotes: 3

Related Questions