I need to check for a link where title="Week 1" or title="Week 13", I have tried somethings what I found on the internet but it won`t work:
So, how to match if something have one number or two numbers?
In order to match one or two digit number, you may use one of the following patterns:
(?<!\d)\d{1,2}(?!\d)
- This expression finds 1 or 2 digits that are not enclosed with any other digit, so it will find a match in a12b
, 1
, 12
, a_1_b
, x_23
strings. See the regex demo.
\b\d{1,2}\b
- This regex is similar to the above one with one exception: it won't match if there are letters or underscores on either end. So, it will match in -1-
, ,23,
but won't match in a_23_b
or a12b
. See the regex demo.
(?<!\S)\d{1,2}(?!\S)
- This expression will match one or two digits ONLY between whitespace characters or start/end of string (within whitespace boundaries). So, it will match in a 12 b
, a 12
, 12 b
, but won't match in 12a
, -12-
, 12_
. See the regex demo.
(?<!\d)(?<!\d\.)\d{1,2}(?!\.?\d)
- This regex will match one or two digits that is not a part of a number, as there cannot be a digit or a digit followed with a dot (you may need to adjust the pattern for other decimal separators) before the two digits, and there cannot be a dot and digit or just a digit after the two digits. See the regex demo.
Yes, if your string is just these two digts, use anchors, ^\d{1,2}$
, or \A\d{1,2}\z
(\A\d{1,2}\Z
in Python re
). Note \z
(\Z
in Python re
) is preferred if you want to handle multiline inputs). See the regex demo.