Home > other >  Python regular expresion how to group last digits and the rest
Python regular expresion how to group last digits and the rest

Time:10-09

So i am trying to get last digits of a text but i need the rest of the group.

I tried using (\d )?$ expression then replacing the word.

import re
text = "kjsdaksjak*?^'{}^^'!11001"
pattern = re.compile(r"(\d )?$")
match = pattern.search(text)
digit = match.group()
rest = text.replace(digit,"")

But is there a better way? Can i group it with regular expressions? I also tried (.*)(\d )?$ but it doesn't work.

CodePudding user response:

You can match the first part of the string before the digits in group 1, and the optional digits at the right in group 2.

Note that the first quantifier is a to prevent matching an empty string.

^(. ?)(\d )?$
  • ^ Start of string
  • (. ?) Capture group 1, match as least as possible chars
  • (\d )? Optional capture group 2, match 1 digits
  • $ End of string.

Regex demo

If you always want to have group 2 in the code (to not check for the existence of group 2 first), you can also match 0 digits instead in group 2:

^(. ?)(\d*)$

Regex demo | Python demo

Then you should first check if there is a match, as re.search can also return None.

import re

text = "kjsdaksjak*?^'{}^^'!11001"
pattern = re.compile(r"^(. ?)(\d*)$")
match = pattern.search(text)

if match:
    print(match.group(1))
    print(match.group(2))

Output

kjsdaksjak*?^'{}^^'!
11001

CodePudding user response:

You can write

import re
r = r'(?=[01] $)'
s = "kjsdaksjak*?^'{}^^'!11001"
print(re.split(r, s, 1))
  #=> ["kjsdaksjak*?^'{}^^'!", '11001']

Demo

The last argument of split is the maximum number of splits, or one less than the maximum number of strings produced by the split. Without that argument the return value would be

["kjsdaksjak*?^'{}^^'!", '1', '1', '0', '0', '1']

The regular expression matches the zero-width location preceding the first digit (a '1') that is followed by zero or more digits at the end of the string.

I assume that the digits are to be 0 or 1. If they can be any digits replace [01] with \d.

  • Related