Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the first word in the string

Tags:

python

regex

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 
like image 404
Vor Avatar asked Dec 06 '12 18:12

Vor


People also ask

How do I get the first word in SQL?

SELECT SUBSTRING_INDEX(yourColumnName,' ',1) as anyVariableName from yourTableName; In the above query, if you use -1 in place of 1 then you will get the last word. To understand the above concept, let us create a table.

How do you find a word inside of a string?

To find a word in the string, we are using indexOf() and contains() methods of String class. The indexOf() method is used to find an index of the specified substring in the present string. It returns a positive integer as an index if substring found else returns -1.

How do I extract words from a string?

To extract words from a string vector, we can use word function of stringr package. For example, if we have a vector called x that contains 100 words then first 20 words can be extracted by using the command word(x,start=1,end=20,sep=fixed(" ")).


1 Answers

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

like image 193
Silas Ray Avatar answered Nov 07 '22 06:11

Silas Ray