0

I have this pattern to be tested in javascript using REGEX:

nn.nnn.nnn./nnnn-nn

where n can be any integer between 0-9.

And I have this regex that works.

[0-9]{2}[.][0-9]{3}[.][0-9]{3}\/[0-9]{4}[-][0-9]{4}

Is there another more elegant way to rewrite this expression to grab the same pattern?

2
  • 1
    Edit your title, Javascript Commented Dec 2, 2014 at 18:38
  • you have a fixed and clean pattern to match, why bothering complicating the regex? Commented Dec 2, 2014 at 18:40

2 Answers 2

1

There's a few possible simplifications:

[0-9] -> \d
[.] -> \.
[-] -> -
nnn.nnn. -> (\d{3}\.){2}

nn.nnn.nnn./nnnn-nn -> \d{2}\.(\d{3}\.){2}\/\d{4}-\d{2}

Your pattern asks for 4 digits at the end, but not your data sample. I followed the sample.

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

2 Comments

Hi plalx, this part was what I was looking for (someting){2}, because I realise that the code I wrote was duplicated, and it could be better written. I had to change a little from your code that is good. I only changed the position of the second \. and deleted the first one. The final is like this: '\d{2}(\.\d{3}){2}\/\d{4}-\d{2}'
the pattern should be nn.nnn.nnn/nnnn-nn not nn.nnn.nnn./nnnn-nn
0

Read my comment, and probably you should correct it to something like:

\d{2}\.\d{3}\.\d{3}\.\/\d{4}-\d{2}

1 Comment

hi DRC I was tring to avoid repeat pattern like \.\d{3} 2 times . So the answer was made by plalx only (something){2}

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.