简体   繁体   中英

Python regular expression for substring

All I want is to grab the first 3 numeric characters of string:

st = '123_456'
import re
r = re.match('([0-9]{3})', st)
print r.groups()[0]
  1. Am I doing the right thing for grabbing first 3 characters?

  2. This returns 123 but what if I want to get the first 3 characters regardless of numbers and alphabets or special characters?

When given 12_345 , I want to grab only 12_

Thanks,

If you always need first three characters in a string, then you can use the below:

first_3_charaters = st[:3]

There is no need of regular expression in your case.

If all digits are separated by _ , then you can simply use this regular expression which greedily matches all numeric characters before the first _ .

r = re.match('([0-9]*)_', st)

Actually, the _ in this RE is not necessary,so you can simplify it to (so that any separator is accepted ):

r = re.match('(\d*)', st)

But this solution will give you 1234 if st = '1234_56' . I'm not sure whether it is your intention.

So, if you want at most 3 numeric characters , you can just modify the regular expression to:

r = re.match('(\d{,3})', st)

You are really close, just drop the extra set of parenthesis and use the proper indexing of zero instead of one. Python indexing starts at zero. See below.

This works:

import re
mystring = '123_456'
check = re.search('^[0-9]{3}', mystring)
if check:
    print check.group(0)

the ^ anchors to the beginning of the string which will ensure a match to the first three numeric digits only. If you do not use the carrot the regexp will match any three digits in a row in the string.

Some may suggest \\d but this includes more than 0-9.

As others will surely point out a simple substring operation will do the trick if all the fields start with three numeric digits that you want to extract.

Good luck!

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