2
\$\begingroup\$

I have a regular expression that match the string that contains the word "duration", followed by a < or > operator and then a number. Here is the expression:

duration\s*(<|>)\s*([0-9]+)

Is there any way I can make this expression better?

200_success
145k22 gold badges190 silver badges478 bronze badges
asked May 10, 2016 at 0:30
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Can you give some example input and output text? \$\endgroup\$ Commented May 10, 2016 at 3:05

2 Answers 2

2
\$\begingroup\$

Since you only have two characters to compete with, namely, < and >, use a character set. Other than that, you might consider \d matching your digits, instead of 0-9 as \d covers the numerical characters across the entire UTF8 range.

The final expression would be:

duration\s*([<>])\s*(\d+)

I've left the matching groups in place, considering that you might be referencing them in your later code.

answered May 10, 2016 at 4:50
\$\endgroup\$
0
\$\begingroup\$

Since you have only duration keyword after that either < or> sign & then the 1 or more numeric value then use below one regex which simple.

duration([<>])(\d+)

I removed the character which are not required.

answered May 10, 2016 at 7:26
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.