Home > Back-end >  Regex match word with alphabets number but not just number
Regex match word with alphabets number but not just number

Time:07-21

For example i've

Jack_50 * 50
Debby_35 * 15
Ross_10 * 24

I want to get just

Jack_50, Debby_35, Ross_10

I tried

[\w._] 

It gives even the *50, *15, *24

[^\d\W] 

This gives just jack,debby, ross

even tried https://www.autoregex.xyz/home

CodePudding user response:

Try this:

import re

s = 'Jack_50 * 50   Debby_35 * 15  Ross_10 * 24'
re.findall(r'[a-zA-Z_] \d ', s)

Output:

['Jack_50', 'Debby_35', 'Ross_10']

You can also try on every separate string like Jack_50 * 50

Explained r'[a-zA-Z_] \d ': matches one or more characters from a-zA-Z_ followed by one or more numbers (\d ). This doesn't match space or *.

CodePudding user response:

Use the following expression:

^\w

The above expression will match one or more occurrences of word characters (alphabets capital or small, digits and underscore) at the starting of the line.

  • Related