user40739
user40739

Reputation: 91

Regular expression starts with a letter ends before a number

Consider the two strings:

a) 'Black cat 39 hello there how are you 06:50?'

b) 'London 123 Welcome to the great city 0092'

I want to extract a pattern in python (using import re) that would return 'Black cat' in the first case and 'London' in the second case. That is, it should return word or sequence of words until it sees the first number. How do I write a regular expression for it? I tried the following which is of course not correct?

^[A-Z][A-Za-z\s]*[0-9]$

Upvotes: 0

Views: 1033

Answers (2)

user9321739
user9321739

Reputation:

You can use the pattern r"^([^\d]+) \d" to match any word up until a number. Lets break it down to better understand the expression.

  • ^ tells re that we want to search from the beginning of the string,
  • the brackets () are used to define a group,
  • the square brackets [] define a set of characters.
  • [^\d] the ^ operator inside of the set means that we want to search for any character that is not a digit.
  • \d means digit.
  • + the plus operator at the end of the set means we want to apply this rule to multiple characters.
  • \d this means stop applying the not a digit character set rule when a digit and space is found.

>>> import re
>>> a = 'Black cat 39 hello there how are you 06:50?'
>>> b = 'London 123 Welcome to the great city 0092'
>>> re.compile(r"^([^\d]+) \d").findall(a)[0]
'Black cat'
>>> re.compile(r"^([^\d]+) \d").findall(b)[0]
'London'
>>> 

Upvotes: 1

Greg W.F.R
Greg W.F.R

Reputation: 734

import re

a = 'Black cat 39 hello there how are you 06:50?'
b = 'London 123 Welcome to the great city 0092'
string = re.search("[^0-9]+" , a)
string2 = re.search("[^0-9]+" , b)
if string is not None:
    print(string.group(0).strip())
if string2 is not None:
    print(string2.group(0).strip())

Upvotes: 0

Related Questions