Home > OS >  Python re.sub() wildcard
Python re.sub() wildcard

Time:05-14

Am I doing something incorrectly with wildcards? I am trying to trim anything that follows " > " but it is only trimming the " > "

import re
noteIndex = "5/10/22, 14:20 > Mr Frank"
subStrip = ' > *'
noteStrip = noteIndex.replace(", " , "/")
noteID = re.sub(subStrip , "" , noteStrip)
print(noteID)

My output ends up being "5/10/22/14:20Mr Frank" when I want it to be "5/10/22/14:20"

CodePudding user response:

Short answer, * is not a 'wildcard' in regex. * means 'match the preceding character (or character class) 0 or more times. So > * means match > followed by 0 or more spaces.

The 'match anything' wildcard character in regex is .. So >.* would match > followed by 0 or more of any character.

  • Related