dear_tzvi
dear_tzvi

Reputation: 745

Regular expressions (regex) - How to split a string by the first X digits appear in it?

I've been struggling to find the right regex (Python) to cover my requirement:

I want to split a string according to the first place in which there are 6 digits.

For example -

stringA = 'abcdf123456789'

Will ideally be cut into -

StringB='abcdf123456'
StringC='789'

So far - This is the solution I came up with:

x = re.split("(?=[0-9])", stringA)

And than loop over the results while counting the chars.

Your help will be greatly appreciated!

Upvotes: 2

Views: 75

Answers (6)

william
william

Reputation: 52

You can just use the findall() and groups aka '()' and simply finds the things you need!

import re

stringA = 'abcdf123456789'
pattern = r"([\D]*\d{6})(.*)"

result = re.findall(pattern, stringA)
print(result)
#output [('abcdf123456', '789')]

Upvotes: 0

anubhava
anubhava

Reputation: 786359

You may use this code with 2 capture groups:

>>> import re
>>> stringA = 'abcdf123456789'
>>> [(stringB,stringC)] = re.findall(r'(.*?\d{6})(.*)', stringA)
>>> print (stringB)
abcdf123456
>>> print (stringC)
789

Upvotes: 1

dawg
dawg

Reputation: 104111

Using a lookbehind:

>>> stringA = 'abcdf123456789'
>>> re.split(r'(?<=\d{6})', stringA, maxsplit=1)
['abcdf123456', '789']

Demo and explanation of the regex

Upvotes: 2

NeedMoreBBQ
NeedMoreBBQ

Reputation: 21

import re
stringA = 'abcdf123456789'
result = re.split("(?<=[0-9]{6})",stringA,maxsplit=1)
print(result)
# ['abcdf123456', '789']

Upvotes: 2

Jan Christoph Terasa
Jan Christoph Terasa

Reputation: 5945

import re

stringA = 'abcdf123456789'
index = re.search(r'\d{6}', stringA).end()
stringB = stringA[:index]
stringC = stringA[index:]

Upvotes: 0

Thierry Lathuille
Thierry Lathuille

Reputation: 24291

You can split on 6 digits with maxsplit=1, and capture the group you split on, then you can build your strings easily:

import re

stringA = 'abcdf123456789'
split = re.split(r'(\d{6})', stringA, maxsplit=1)
# split is now ['abcdf', '123456', '789']
stringB = ''.join(split[:2])
stringC = split[2]

print(stringB)
print(stringC)

# abcdf123456
# 789

Upvotes: 1

Related Questions