1

I need to extract certain part of Javascript string. I was thinking to do it with regex, but couldn't come up with one which does it correctly.

String can have variable length & can contain all possible characters in all possible combinations.

What I need to extract from it, is 10 adjacent characters, that match one of next two possible combinations:

  1. 9 numbers & 1 letter "X" (capital letter "X", not X as variable letter!)
  2. 10 numbers

So, if input string is this: "[1X,!?X22;87654321X9]ddee", it should return only "87654321X9".

I hope I've explained it good enough. Thanks in advance!

2 Answers 2

3

This Regex will work:

\d{9}X|\d{8}X\d|\d{7}X\d{2}|\d{6}X\d{3}|\d{5}X\d{4}|\d{4}X\d{5}|\d{3}X\d{6}|\d{2}X\d{7}|\d{1}X\d{8}|\d{10}|X\d{9}

As described, It need to match 9 digits and any letter, and the letter can be at any position of the sequence.

\d{9}X # will match 9 digits and a letter in the end
\d{8}X\d # will match 8 digits a lettter then a digit again
... 
\d{1}X\d{8} # will match 1 digits a lettter then 8 digits
\{10} # will match 10 digits

Edited to match only X

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

4 Comments

sorry, but you didn't understand me - "X" doesn't represent ANY letter, but just CAPITAL letter X (eks). This works for any letter, which is not acceptable
it doesn't work for sequence starting with "X" - "X123456789" for example.
Yes, I don't know if OP wanted a sequence starting with a X, as I stated in my previous comment, he shoult append |X\d{9} at the end
i've appended it, now it works as it should. please edit your answer to include it, just for convinience and other users. thanks again.
2

You can use this much simpler regex:

/(?!\d*X\d*X)[\dX]{10}/

RegEx Breakup:

(?!\d*X\d*X)  # negative lookahead to fail the match if there are 2 X ahead
[\dX]{10}     # match a digit or X 10 times

Since more than one X is not allowed due to use of negative lookahead, this regex will only allow either 10 digits or ekse 9 digits and a single X.

RegEx Demo

This regex has few advantages over the other answer:

  • Much simpler regex that is easier to read and maintain
  • Takes less than half steps to complete which can be substantial difference on larger text.

8 Comments

thanks, can you tell me how to implement it, for example if input string is var input, how can I get output string (var output) using this regex?
It will not match 87654321X9
@anubhava yes it seems to work, but I've already accepted Koala 's answer. Thanks for your effort though!
@anubhava Your regex will match 87XXX421X9
Not quite correct - I tried this in Regex101 87123X219X0 and it matched the first 9 characters 87123X219.
|

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.