1

I want to remove number containing between 6 and 8 digits, so the regex I am using is: \b\d{6,8}

It works fine, but, it if I have two numbers separated by an underscore (_), for example 1234567890_12345678901234567890 I want it removed as well. I must use \b (boundary).

To me it seems like a condition:

match numbers between 6 and 8 digit, but if you see two numbers separated by an underscore match them too (regardless of the number of digits in each number).

match: 12345678

match: 12345678934567_1234567890123456789

match: 123_23

no match: 12345

I need a single regex that handles both cases.

Thanks a lot.

0

3 Answers 3

3

Try the following:

\b(?:\d{6,8}|\d+_\d+)\b 

It is simply 6 to 8 digits or any number_number.

Click here to see it in action.

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

3 Comments

Hi Neil, I can't use the last \b, I need something that will work without the last \b Thanks
@AsafCohen If you remove the last \b, then it will match numbers larger than 8 digits. If it is followed by letters rather than numbers, then you could also replace \b with (?:[^_\d]|$). See here.
This is what I needed.Thanks!
1

You may use this

^(\d+_\d+)|(\d{6,8})$ 

This regex contains two parts:

  1. (\d+_\d+) covers cases with "_" symbol;

  2. (\d{6,8}) covers other cases

3 Comments

That would match ______, _1234_, etc.
You don't need character class in [\d]
@Tushar, thanks for this notice. I've corrected my answer.
0

You can try this one as well:

\b\d+(?:\d{4,6}|_)\d+\b 

And if you'd like to allow more than one _ characters, try this one:

\b\d+(?:\d{4,6}|_[\d_]*)\d+\b 

This second one will match on this too: 12345678934567_1234567890_123456789

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.