Skip to content Skip to sidebar Skip to footer

Detection Of Only The Number In Regex

I have the following regex : (?

Solution 1:

You may use

(?<!__num)(?<!\d)10(?!\d)

See the regex demo

The first two negative lookbehinds will be executed at the same location in a string and (?<!__num) will make sure there is no __num immediately before the current location and (?<!\d) will make sure there is no digit.

The (?!\d) negative lookahead will make sure there is no digit immediately after the current location (after a given number).

Python demo:

import re
# val = "110" # => <_sre.SRE_Match object; span=(14, 17), match='110'>
val = "10"
s = "can there be B110 numbers and 10 numbers in this sentence"print(re.search(r'(?<!__num)(?<!\d){}(?!\d)'.format(val), s))
# => <_sre.SRE_Match object; span=(30, 32), match='10'>

Post a Comment for "Detection Of Only The Number In Regex"