Home > Blockchain >  Trying to compare strings into regular expressions patterns
Trying to compare strings into regular expressions patterns

Time:12-29

How can I compare a regex pattern into a String?

Example: "Street A 2 b"

for this case I use:

re.split("([0-9] )", address)

However, for another case "Street 23" I use

re.split("([0-9] )", address)

I need to build this like the two cases in a way that they can talk to each other. If I input "Street A 2 b" I need the output ['Street A', '2 b'] If I input "Street 23" I need the output ['Street', '23']

I tried to do:

address = str(input('Enter the address: '))

if address != re.search(r"([0-9]  [a-z] )", address):
    my_list = re.split("([0-9] )", address)
    print(my_list)
else:
    my_list = re.split("([0-9]  [a-z] )", address)
    print(my_list)

But it's not working

I need the program identify each of the two cases and output correctly.


CodePudding user response:

You can get the result you want using a lookahead in the pattern. This will use a space that's followed by a number as the delimiter.

def split_address(address):
    return re.split(r'\s (?=\d)', address)

print(split_address('Street A 2 b'))
print(split_address('Street 23'))

CodePudding user response:

You could split on the first whitespace number, but only once with maxsplit=1. but this leaves you with 3 element list, so you have to join the end back together. Not sure if it is possible to do it without the joining. E: Stole the lookahead pattern from Barmar's answer.

import re

address = input('Enter the address: ')

my_list = re.split(" (?=[0-9])", address, maxsplit=1)
print(my_list)

FYI: input should already return a str so no need to convert it.

CodePudding user response:

You can use the regex features:

import re

match = re.match(r"(\D )(.*)", "Street A 2 b")

print("'%s', '%s'" % (match[1].strip(), match[2].strip()))

Output

'Street A', '2 b'

Notes

Pattern \D searches for all successive non-digit characters. The matching pattern uses grouping (using parentheses). So the first part (street) is stored in match[1], and the remaining part (number...) in match[2].
To remove unwanted spaces strip() is used.

  • Related