Regex Matching 5-Digit Substrings Not Enclosed with Digits

Regex matching 5-digit substrings not enclosed with digits

Your current regex (((\D|^)*)\d\d\d\d\d((\D|$)*)) used with re.findall won't return the digit chunks because they are not captured. More, the (\D|^)* and
(\D|$)* parts are optional and that means they do not do what they are supposed to do, the regex will find 5 digit chunks inside longer digits chunks.

If you must find 5 digit chunk not enclosed with other digits, use

re.findall(r"(?<!\d)\d{5}(?!\d)", s)

See the regex demo

Details:

  • (?<!\d) - no digit is allowed before the current location
  • \d{5} - 5 digits
  • (?!\d) - no digit allowed after the current location.

Python Regular Expression Match All 5 Digit Numbers but None Larger

>>> import re
>>> s="four digits 1234 five digits 56789 six digits 012345"
>>> re.findall(r"\D(\d{5})\D", s)
['56789']

if they can occur at the very beginning or the very end, it's easier to pad the string than mess with special cases

>>> re.findall(r"\D(\d{5})\D", " "+s+" ")

Python 3 - Regular expression for a set digit length?

You want this:

abc_\d{N}\.txt

Where N is the number of digits.

Re-replacing in regex

So you need a different regex rule. You don't want to replace 2. You want to replace 2 when it's not next to another number or ~.

In order to do this, you can use lookaheads and lookbehinds (although lookbehinds are not yet supported by regexes in JS, I believe, but at least with lookaheads) :

const input = "This is the replacement of ~26~ and ~524~. We still have 2 cadets left. Have2go for the next mission.2";

const regex = /2(?![\d~])/gm // Means : "2 when it's not followed by a digit \d or a ~"

console.log( input.replace(regex, "~86~" ) )

Regular expression to match a phone number in string

(?<!\d)\d{3}\-\d{3}\-\d{4}(?!\d)

https://regex101.com/r/VAaA7k/1

phoneNumRegex = re.compile(r'(?<!\d)\d{3}\-\d{3}\-\d{4}(?!\d)')

https://www.ideone.com/4Ajfqe


To prevent other unwanted matches, use a stronger regex pattern

(?:^|:|(?<=\s))(\d{3}\-\d{3}\-\d{4})(?=[\s,;.]|$)

https://regex101.com/r/VAaA7k/5

Capture all numbers up to three digits

Add word boundaries:

import re

result = re.findall(r'\b\d{1,3}\b', '1 2 134 2009')

print(result)

Output

['1', '2', '134']

From the documentation \b:

Matches the empty string, but only at the beginning or end of a word.
A word is defined as a sequence of word characters. Note that
formally, \b is defined as the boundary between a \w and a \W
character (or vice versa), or between \w and the beginning/end of the
string. This means that r'\bfoo\b' matches 'foo', 'foo.', '(foo)',
'bar foo baz' but not 'foobar' or 'foo3'.

By default Unicode alphanumerics are the ones used in Unicode
patterns, but this can be changed by using the ASCII flag. Word
boundaries are determined by the current locale if the LOCALE flag is
used. Inside a character range, \b represents the backspace character,
for compatibility with Python’s string literals.

php regex - how to get all two digits

Looks like you want to capture 2 digits that should be preceded and followed by any other digits. You may use:

(?<!\d)\d{2}(?!\d)

RegEx Details:

  • (?<!\d): Assert that previous character is not a digit
  • \d{2}: Match 2 digits
  • (?!\d): Assert that next character is not a digit

RegEx Demo

Find the first set of 5 digits in a text

To match the first 5-digit number you may use

^.*?\K(?<!\d)\d{5}(?!\d)

See the regex demo. As you want to remove the match, simply keep the Replace With field blank. The ^ matches the start of a line, .*? matches any 0+ chars other than line break chars, as few as possible, and \K operator drops the text matched so far. Then, (?<!\d)\d{5}(?!\d) matches 5 digits not enclosed with other digits.

Another variation includes a capturing group/backreference:

Find What:      ^(.*?)(?<!\d)\d{5}(?!\d)

Replace With: $1

See this regex demo.

Here, instead of dropping the found text before the number, (.*?) is captured into Group 1 and $1 in the replacement pattern puts it back.



Related Topics



Leave a reply



Submit