0

I'm looking to see if the following string(s) match the number 1 in them

23_13_15_145_34_1_135

23_13_15_145_34_1

1_23_13_15_145_3

I only want to match it exactly so the first example must not include the 13, 15 or 145, just the 1.

Same with the start and end examples with the 1 at the start and end of the string.

1
  • 1
    What have you tried so far? This site does expect you to do SOME work on your own, since we pretty much all universally hate people who freeload and won't do their own work. Commented Jul 9, 2012 at 21:50

2 Answers 2

2

Try this:

/(^|_)1(_|$)/

It checks that 1 is preceded immediately by the beginning of the string (^) or a _ character, and that it is followed immediately by another _ character, or the end of the string ($).

And of course, add ?: to each grouping to avoid capturing, if you care (and your RE engine supports it):

/(?:^|_)1(?:_|$)/
Sign up to request clarification or add additional context in comments.

Comments

0

The following should work (uses lookaround):

(?<!\d)1(?!\d)

This regex will match a single 1 character, but only if there are not any digits before or after it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.