Python

Is it possible to implement something like this simple in Python:

#!/usr/bin/perl
my $a = 'Use HELLO1 code';
if($a =~ /(?i:use)\s+([A-Z0-9]+)\s+(?i:code)/){
    print "$1\n";
}

      

The marker letters in the middle of the line are always capital. The letters of the rest of the words can have any case (use, use, use, code, code, code, etc.)

+2


source to share


3 answers


As far as I can find, the python regex engine does not support partial ignore case. Here is a solution using a case insensitive regex, which then checks if the token is uppercase after that.

#! /usr/bin/env python

import re

token_re = re.compile(r'use\s+([a-z0-9]+)\s+code', re.IGNORECASE)
def find_token(s):
    m = token_re.search(s)
    if m is not None:
        token = m.group(1)
        if token.isupper():
            return token

if __name__ == '__main__':
    for s in ['Use HELLO1 code',
              'USE hello1 CODE',
              'this does not match',
             ]:
        print s, '->',
        print find_token(s)

      



Here is the output of the program:

Use HELLO1 code -> HELLO1
USE hello1 CODE -> None
this does not match -> None

      

+9


source


According to the docs, this is not possible. The syntax (?x)

allows you to change the flag for the entire expression. Hence, you have to break it down into three regular expressions and apply them one by one, or do "ignore case" manually:/[uU][sS][eE]...



+3


source


As of python 3.6, you can use a flag inside groups:

(imsx-imsx: ...)

(Zero or more letters from the set "i", "m", "s", "x", optionally followed by "-", followed by one or more letters from the same set.) Letters are set or removed corresponding flags : re.I (ignore case), re.M (multiline), re.S (dot matches all) and re.X (verbose), for part of an expression.

So (?i:use)

now is the correct syntax. From python3.6 terminal:

>>> import re
>>> regex = re.compile('(?i:use)\s+([A-Z0-9]+)\s+(?i:code)')
>>> regex.match('Use HELLO1 code')
<_sre.SRE_Match object; span=(0, 15), match='Use HELLO1 code'>
>>> regex.match('use HELLO1 Code')
<_sre.SRE_Match object; span=(0, 15), match='use HELLO1 Code'>

      

+1


source







All Articles