regex.sub() gives different results to re.sub()

前端 未结 2 744
栀梦
栀梦 2021-01-04 05:07

I work with Czech accented text in Python 3.4.

Calling re.sub() to perform substitution by regex on an accented sentence works well, but using a regex compiled with

2条回答
  •  旧巷少年郎
    2021-01-04 05:28

    The last argument in the compile is flags, if you actually use flags=flags in the re.sub you will see the same behaviour:

    compiled = re.compile(pattern, flags)
    print(compiled)
    text = 'Poplatníkem daně z pozemků je vlastník pozemku'
    mark = r'**\1**' # wrap 1st matching group in double stars
    
    r = re.sub(pattern, mark, text, flags=flags)
    

    The fourth arg to re.sub is count so that is why you see the difference.

    re.sub(pattern, repl, string, count=0, flags=0)

    re.compile(pattern, flags=0)

提交回复
热议问题