Reputation: 804
I have the following string:
Datum Kundnummer Sida
2018-10-12 196979 1 /2
The two strings above are in separate lines
What is the regex equivalent so that I get the following output?
Datum 2018-10-12
Kundnummer 196979
The above two are separate outputs
I want the output such that it works for all dates in datum and all numbers in Kundnummer.
I tried Datum\s([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))
and Kundnummer\s(\d+)
.
Upvotes: 1
Views: 85
Reputation: 133
I assume the data in the first line is always the same, so I'd ignore it.
To get the "Datum" and "Kundennummer" entries from the second line, use (?P<datum>\d{4}\-\d{1,2}\-\d{1,2})\s(?P<kundennummer>\d+?).*
.
As @PyHunterMan explained, you could also just split the string at the whitespaces. That would probably be the easier way.
Upvotes: 0
Reputation: 4887
I suggest something like this:
reobj = re.compile(r"(?P<datum>\d{4}-\d{1,2}-\d{1,2})\s+(?P<kundnummer>\d+)")
match = reobj.search(input)
if match:
result1 = match.group("datum")
result2 = match.group("kundnummer")
Upvotes: 0
Reputation:
Why would you wage on time figuring this out when you can do it in a simple lines of code without regex.
_input = "Datum Kundnummer Sida\n2018-10-12 196979 1 /2"
lines = _input.split('\n')
old_line_one, old_line_two = lines[0].split(), lines[1].split()
new_line_one = f'{old_line_one[0]} {old_line_two[0]}'
new_line_two = f'{old_line_one[1]} {old_line_two[1]}'
print(f'{new_line_one}\n{new_line_two}')
Upvotes: 1