user1467723
user1467723

Reputation: 35

Regex for exact number / sting match in between

I want to make a regex where I can find the exact number in between a string.

eg. finding the number 2 in 3, 5, 25, 22,2, 15

What I have is /*,2,*/.

But with this regex it matches 22,25 or just anything with a 2 in it. I want it where only match where the number 2 itself is between the commas or without the commas standing alone.

*Update Both the number(needle) i look for and string(haystack) where i seek it can vary.

Eg if the number i seek is always 2

      I want to find them in 2,3,44,23,22,1 or 3,4,22,5,2 or 2 and i should be able to find one match for each of the group of numbers.

Upvotes: 1

Views: 474

Answers (2)

Jonathan Kuhn
Jonathan Kuhn

Reputation: 15301

You should probably use boundaries (\b) so a leading/trailing comma isn't required.

/\b2\b/

Upvotes: 1

sshashank124
sshashank124

Reputation: 32189

You should do this instead:

,(\d), #for any single digit

,(2), #for 2 in particular

Demo: http://regex101.com/r/vP6jI1

Upvotes: 1

Related Questions