简体   繁体   中英

Split string using regular expression, how to ignore apostrophe?

I am doing a spell check tutorial in Python and it uses this regular expression:

import re
def split_line(line):
    return re.findall('[A-Za-z]+(?:\`[A-Za-z)+)?',line)

I was wondering if you could help me change this function so it will ignore ' , ie if I input the string he's i will get ['he's'] and not ['he','s'] .

First you'll need to fix the original expression by replacing ) with ] as mentioned by Marcin. Then simply add ' to the list of allowed characters (escaped by a back-slash):

import re
def split_line(line):
    return re.findall('[A-Za-z\']+(?:\`[A-Za-z]+)?',line)

split_line("He's my hero")

#["He's", 'my', 'hero']

Of course, this will not consider any edge cases where the apostrophe is at the beginning or at the end of a word.

Your regex is supposed to match one or more letters and then an optional occurrence of a backtick and again one or more letters. You can put the backtick into a character class and add ' into the class.

Note that you do not need to escape ' if you use a double-quoted string literal:

re.findall(r"[A-Za-z]+(?:['`][A-Za-z]+)*", line)

See the regex demo . Details :

  • [A-Za-z]+ - one or more ASCII letters (use [^\\W\\d_]+ to match any one or more Unicode letters)
  • (?:['`][A-Za-z]+)* - zero or more occurrences of ' or backtick followed with one or more ASCII letters.

See the Python demo :

import re
text = "And he's done it o`key!"
print(re.findall(r"[A-Za-z]+(?:['`][A-Za-z]+)*", text))
# => ['And', "he's", 'done', 'it', 'o`key']

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