0

This question might be dumb, I'm new to regex and I am a bit stuck.

I have multiple strings, with numbers and words in them. I want to match only the words, if they do not contain an integer.

12 This Is A Test 9eifEf 12

From that, I would like to match This Is A Test.

11 Stack 21deEh 12

From that, I would like to match Stack.

Using RegExr, I came up with the expression .[a-z], which looked like it worked, but it matched a maximum of 2 letters at a time, and not the spaces.

Sorry for the code request. I do not need anything more than the pattern. I appreciate any help.

Will
  • 4,942
  • 2
  • 22
  • 47
  • Split the string and check if all the characters are letters (`[x for x in s.split() if x.isalpha()]`). Or is the requirement more specific? – Wiktor Stribiżew Feb 03 '17 at 12:19
  • Did you understand why your regex is false? The `.` will match any character (which is not what you want), the `[a-Z]` will only match one lowercase letter, and you forgot `\b` word delimiters. See Willem's post for the solution. – Raphaël Gomès Feb 03 '17 at 12:23

2 Answers2

3

Simply use:

\b[A-Za-z]+\b

Here:

  • \b is a word boundary such that we do not match words that begin with digits;
  • [A-Za-z] is a character group that contains all upper and lower letters; and
  • + means "one or more".

If you want to return the string then, you can - as @James says - use ' '.join(..):

$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> rgx=re.compile(r'\b[A-Za-z]+\b')
>>> text='12 This Is A Test 9eifEf 12'
>>> rgx.findall(text)
['This', 'Is', 'A', 'Test']
>>> ' '.join(rgx.findall(text))
'This Is A Test'
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
2

No need for regex, use str.isalpha to filter out digits out of splitted words:

s = "12 This Is A Test 9eifEf 12"

print(" ".join([x for x in s.split() if x.isalpha()]))

gives:

This Is A Test

this won't preserve multiple spaces, though. To do it, just do:

print(" ".join([x for x in s.split(" ") if not x or x.isalpha()]))
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219