2

My regex knowledge is strictly limited, but I need to use it to check a string for a specific number, e.g.

12345 123543 123222 4124214 2323 42124 23123 24444 34342

How can I use regex to confirm whether 2323 is in the above string?

Basically, if the string contains the number, for my project, then the string and the number are a match. If this isn't clear, please let me know and I'll try to expand.

1
  • Does it have to match a full number? For example, is 4444 in your example string? Commented May 16, 2013 at 0:38

1 Answer 1

4

The regex for checking if 2323 is in the string is:

.*\b2323\b.*

This means anything, then 2323, then anything. The \bs will make sure that the 2323 is not in the middle of a number, but its own number. (Without the \b word boundaries, the regex would match 83523238 because it has 2323 in it.)

Sign up to request clarification or add additional context in comments.

2 Comments

That's great, thank you very much, will it ensure that spaces are used to separate numbers in the string? As I can't allow 2323 to match 152323 etc
@JeremyHewitt That's why the \bs are there

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.