0

I need to validate that strings has the following pattern:

component/1.2/text
component/1.2.3/othertext
component/1.2.32/text
component/10.21.32/yetanothertext

So always contains two path separators, starts with component followed by major.minor or major.minor.bug and ends with some text.

Any suggestion for a regular expression that can be used to discard anything NOT following the above pattern, e.g.:

component/4/base
comp/4/samples
...
2
  • So, what have you tried? Commented Jun 24, 2016 at 9:14
  • /^component\/\d+(?:\.\d+){1,2}\/\w+$/ ... regex101.com/r/aE8cP4/1 Commented Jun 24, 2016 at 9:14

2 Answers 2

2

Try this regex. ^component\/\d+(?:\.\d+){1,2}\/\w+

Here's a visualization.

Regular expression visualization

Try demo here.

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

Comments

0

and another soultion:

component/([\d\.]{1,2})/\w+

Or you can try also with {0,1}, not {1,2}

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.