Python Regex Expression that finds variable string between whitespace, punctuation and/ or string end

I need the regex expression to find a substring (it’s a variable) that is either preceded/followed by punctuation, whitespaces or the start/end of the string. I don’t know the size or content of the substring. I’ve come up with [\?\.!\- ]1abc[\?\.!\- ] (this is a specific example where the substring is 1abc) but I don’t… Read More Python Regex Expression that finds variable string between whitespace, punctuation and/ or string end

python regex lookbehind to remove _sublabel1 in string like "__label__label1_sublabel1"

i have dataset that prepare for train in fasttext and i wanna remove sublabels from dataset for example: __label__label1_sublabel1 __label__label2_sublabel1 __label__label3 __label__label1_sublabel4 sometext some sentce som data. Any help much appreciated thanks im tried this: r'(?<=__label__[^_]+)\w+’ isnt working exact code: ptrn = r'(?<=__label__[^_]+)\w+’ re.sub(ptrn, ”, test_String) and this error was occured: error: error Traceback (most… Read More python regex lookbehind to remove _sublabel1 in string like "__label__label1_sublabel1"

More elegant way of replacing substring of text that was matched with regex

I am new to python so I would like to get a few ideas for this. I am writing a function to find matching word patterns in a sentence and replace the spaces inside of only the matched words. Input: (c)variable < var_CONST1(例 -125(N)) 【AAA BBB有】AND【技術企画】AND【AAA BBB CCC】 Expected Output: (c)variable < var_CONST1(例 -125(N)) 【AAA-BBB有】AND【技術企画】AND… Read More More elegant way of replacing substring of text that was matched with regex