简体   繁体   中英

Using regex to extract first one letter word

I want to extract the first one letter word from a string after a number. Essentially the suffix of a street number. Eg: 1A some st Some city some postcode, or U 1A some address, APT 5 A 56 Some st some city. So first one letter after a number. Would someone help me figure out how can I use the condition in pandas column using regex.

Many Thanks, Sruthy

So if I'm understanding your question right, you want to find the any single letter that comes after a number. In other words, you're looking for a number, followed by a letter, followed by a whitespace. The following regex will get that for you:

\d+([a-zA-Z])\s

What this does is, the \d matches a digit, and the + makes it so that it can match more than once, so that multi-digit numbers will match. The next part in parenthesis, matches any character between az in lowercase and upper case. The last part, \s , matches any whitespace character.

I hope this is what you were looking for, and I hope this helps!

Given, s is a pandas series, you can apply-

s.str.extract(r'(?P<digit>\d)({1}P<letter>[A-Za-z])')

This will return two columns with digits and letters in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM