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?

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Can you give some example input and output text? \$\endgroup\$
    – Zack
    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.

\$\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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.