Reputation: 141
How can I match the the pattern any number followed by h or t or l
like 1h , 126h or 1268h
but not 1.1h 12.6h or 12.68h
in a given paragraph.
I am writing a application that can replace 1h
to 100
or 1268h
to 126800
so that instead of typing 00
a person can simply place h
with a number but due to some error it is also matching decimal numbers, too.
Pattern that I wrote is (\d+)(h|t|l)
Upvotes: 0
Views: 44
Reputation: 163247
You could use a whitespace boundary to the left (?<!\S)
if a positive lookbehind is supported or anchors to match the whole line.
The alternation can be written as a character class [htl]
(?<!\S)(\d+)([htl])
(?<!\S)
Positive lookbehind, assert what is on the left is not a non whitespace char(\d+)
Capture group 1, match 1+ digits([htl])
Capture group 2, match either h
tor
l`Using anchors to match the whole line
^(\d+)([htl])$
Without a lookaround, you could match either a whitespace char or the start of the string (?:\s|^)
for example:
(?:\s|^)(\d+)([htl])
Upvotes: 2