How to get the first word in the string

PythonRegex

Python Problem Overview


text is :

WYATT	 - Ranked #	855	with	0.006	%
XAVIER	 - Ranked #	587	with	0.013	%
YONG	 - Ranked #	921	with	0.006	%
YOUNG	 - Ranked #	807	with	0.007	%

I want to get only

WYATT
XAVIER
YONG
YOUNG

I tried :

(.*)?[ ]

But it gives me the :

WYATT    - Ranked

Python Solutions


Solution 1 - Python

Regex is unnecessary for this. Just use some_string.split(' ', 1)[0] or some_string.partition(' ')[0].

Solution 2 - Python

If you want to feel especially sly, you can write it as this:

(firstWord, rest) = yourLine.split(maxsplit=1)

This is supposed to bring the best from both worlds:

I kind of fell in love with this solution and it's general unpacking capability, so I had to share it.

Solution 3 - Python

You shoud do something like :

print line.split()[0]

Solution 4 - Python

Use this regex

^\w+

\w+ matches 1 to many characters.

\w is similar to [a-zA-Z0-9_]

^ depicts the start of a string


About Your Regex

Your regex (.*)?[ ] should be ^(.*?)[ ] or ^(.*?)(?=[ ]) if you don't want the space

Solution 5 - Python

Don't need a regex. string[: string.find(' ')]

Solution 6 - Python

You don't need regex to split a string on whitespace:

In [1]: text = '''WYATT    - Ranked # 855 with    0.006   %
   ...: XAVIER   - Ranked # 587 with    0.013   %
   ...: YONG     - Ranked # 921 with    0.006   %
   ...: YOUNG    - Ranked # 807 with    0.007   %'''

In [2]: print '\n'.join(line.split()[0] for line in text.split('\n'))
WYATT
XAVIER
YONG
YOUNG

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVorView Question on Stackoverflow
Solution 1 - PythonSilas RayView Answer on Stackoverflow
Solution 2 - PythonHugeView Answer on Stackoverflow
Solution 3 - PythonNadoView Answer on Stackoverflow
Solution 4 - PythonAnirudhaView Answer on Stackoverflow
Solution 5 - PythonRicardo Alvaro LohmannView Answer on Stackoverflow
Solution 6 - PythonLev LevitskyView Answer on Stackoverflow