veaba <908662...@qq.com> added the comment: Yes, this is not a good place to use regular expressions.
Using regular expressions: def actual_re_demo(): import re # This is an indefinite string... text = "tf.where(condition, x=None, y=None, name=None) tf.batch_gather ..." # Converting fields that need to be matched into regular expressions is also an indefinite string pattern_str = re.compile('(tf\\.batch_gather)|(None)|(a1)') #I don't know how many, so it's over \ \ 100 \ \ n x = re.sub(pattern_str, '`'+'\\1\\2'+'`', text) print(x) # hope if:tf.Prefix needs to match,The result will be:`tf.xx`, # But in fact, it's not just TF. As a prefix, it's a random character, it can be a suffix, it can be other characters. # If more than 100, the result is=>:989¡¢£¤¥¦§89¨©ª«¬®¯89°±²³´µ¶·89¸¹º»¼½¾¿890123`, name=`None@ABCDEFG89HIJKLMNO89PQRSTUVW89XYZ[\]^_89`abcdefg89hijklmno89pqrstuvw89xyz{|}~8901234567890123456789 # I noticed in the comment area that it was caused by a confusion of Radix, which seems to be embarrassing. Use replace to solve it. It looks much better. def no_need_re(): text = "tf.where(condition, x=None, y=None, name=None) tf.batch_gather ..." pattern_list = ['tf.batch_gather', 'None'] for item in pattern_list: text=text.replace(item, '`'+item+'`') print(text) no_need_re() Expect to report an error directly if it exceeds the limit, instead of overflowing the character, like this: 989¡¢£¤¥¦§89¨©ª«¬®¯89°±²³´µ¶·89¸¹º»¼½¾¿890123`, name=`None@ABCDEFG89HIJKLMNO89PQRSTUVW89XYZ[\]^_89`abcdefg89hijklmno89pqrstuvw89xyz{|}~8901234567890123456789 ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue38582> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com