Python regex expansion, arbitrary length integer

I am trying to make a simple regex separation in Python. The string is in the form FooX, where Foo is some string and X is an arbitrary integer. I have a feeling that it should be very simple, but I cannot get it to work.

In this post, can anyone recommend some good Regex reading material?

+3
source share
5 answers

You cannot use split(), as this should consume some characters, but you can use the usual match for this.

>>> import re
>>> r = re.compile(r'(\D+)(\d+)')
>>> r.match('abc444').groups()
('abc', '444')
+5
source

Using groups:

import re

m=re.match('^(?P<first>[A-Za-z]+)(?P<second>[0-9]+)$',"Foo9")
print m.group('first')
print m.group('second')

Using Search:

import re

s='Foo9'
m=re.search('(?<=\D)(?=\d)',s)
first=s[:m.start()]
second=s[m.end():]

print first, second
+1

:

>>> import re
>>> a = "Foo1String12345"
>>> re.split(r'(\d+)$', a)[0:2]
['Foo1String', '12345']
+1

, "Foo" , - :

r/(?<=\D)(?=\d)/

, .

0
source
>>> import re
>>> s="gnibbler1234"
>>> re.findall(r'(\D+)(\d+)',s)[0]
('gnibbler', '1234')

In a regular expression, \ D means everything that is not a number, so \ D + matches one or more things that are not numbers.

Similarly, \ d means everything that is a digit, so \ d + matches one or more digits

0
source

Source: https://habr.com/ru/post/1730465/


All Articles