2
\$\begingroup\$

Check for Expiry

  • Expiry can go from 1 to 299
  • Expiry can be 1 digit, 2 digits or 3 digits

This will be the Input I check my regExp against.

Rewritet 192.168.3.99, 57714, 192.168.3.3, 22) [*0 2] i0 exp276
Rewritet 192.168.3.99, 57714, 192.168.3.3, 22) [*0 2] i0 exp82
Rewritet 192.168.3.99, 57714, 192.168.3.3, 22) [*0 2] i0 exp2

exp will decrease every second by one

My RegExp

let regExp = \exp[0-2]{0,1}[0-9]{1,2}\

The regeExp matches everything up from 0 up to 299.

Try it here

What do you think about this regExp.

I'm looking forward to any advice about Regular Expressions

\$\endgroup\$

3 Answers 3

1
\$\begingroup\$

Shortcuts You can use 2 special regex syntax rules to make the pattern more concise:

  • When a token may or may not be matched, instead of {0,1}, it can be done with ? instead - they're exactly equivalent.

  • When you want to match a digit from 0 to 9, rather than writing out [0-9], you can use \d instead (they're exactly equivalent):

Typo? Regular expressions in JS are delimited by forward slashes, not backslashes.

Prefer const - Unless you plan on reassigning the regExp variable name, better to use const - only use let when you need to warn readers that the variable may be reassigned.

Missing semicolon Unless you're an expert and can avoid the pitfalls of Automatic Semicolon Insertion, I'd highly recommend using semicolons whenever appropriate, else you will occasionally run into hard-to-understand bugs.

Bug? You probably only want to match digits if they fulfill all the rules - I doubt you want to match the 30 part of 300, for example. I'd expect that you want a line that ends with 300 to not match at all, right? Fix it by adding a word boundary at the end:

const regExp = /exp[0-2]?\d{1,2}\b/;

Or, if the digits will always come at the end of the line, use $ and the m flag instead:

const regExp = /exp[0-2]?\d{1,2}$/m;

https://regex101.com/r/udp9BX/7

\$\endgroup\$
2
  • \$\begingroup\$ Thank you a lot. I updated my RegEx. I understand the bug with my RegEx I also recognized it while testing around but I thought it is fine because this will not happen. But I'm interested in the word boundary, what does this? \$\endgroup\$
    – Aalexander
    Commented Dec 10, 2020 at 14:24
  • \$\begingroup\$ A word boundary will be matched if the position is between a word character and not a word character. For example, 300 doesn't match 30\b0 because the \b there is between two word characters. (Numbers, letters, and underscores are all word characters.) \$\endgroup\$ Commented Dec 10, 2020 at 14:27
0
\$\begingroup\$

What is the volume of usage? ie 86400 time per day per user? How many users? 1, 100, 1000? Regular Expressions are slow, but slow is ok if the volume is low.

Your regex is fine, though you meant / I believe. Depending on your needs this maybe a slight improvement.

const regExp = /exp(?<seconds>[0-2]?\d{1,2})/

const tests = [
    `Rewritet 192.168.3.99, 57714, 192.168.3.3, 22) [*0 2] i0 exp276`,
    `Rewritet 192.168.3.99, 57714, 192.168.3.3, 22) [*0 2] i0 exp82`,
    `Rewritet 192.168.3.99, 57714, 192.168.3.3, 22) [*0 2] i0 exp2`,
]

const results = tests.map(test=>parseInt(regExp.exec(test)?.groups?.seconds))

console.log(results)

Secondly, a good IDE is important. While this suggestion likely has little to no performance benefit, I do feel it better expresses that fact that the first digit is optional. I considered doing the same for the second digit.

let regExp = /exp[0-2]{0,1}[0-9]{1,2}/
//                     ^-- IDE flagged this as 'Repetition range replaceable by ?'
\$\endgroup\$
0
\$\begingroup\$

Based on your explanation of requirements and your sample data, I get the impression that the only number that may start with a zero is 0.

For this reason, the pattern can be tightened to prevent unwanted leading zeros.

/exp(?:[12]\d|[1-9])?\d$/m
  • the hundreds digit is optional, but must be a one or a two, then followed by two digits, then the end of the line.
  • the tens digit is optional, but must be between one and nine, then followed by a digit, then the end of the line.
  • the ones digit is required, it can be from zero to nine, then the end of the line.
\$\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.