Pretty obviously written by somebody who wanted to split a string while keeping the delimiters, stumbled upon this stackoverflow answer, and got a little bit lost along the way :)
Here's how I would break it down instead:
regex = r'({\w})' # match {a}, {b}, {c}, etc
result = re.split(regex, tex) # split by regex matches, keeping any matches within a capture group
result = list(filter(None, result)) # remove empty strings from list
result = list(map(lambda el: (el, bool(re.match(regex, el))), result)) # map result to a list of tuples containing the element, and whether or not it is a placeholder
The comments in OP's code contradict their regex, so I simplified it. Obviously they can plug in their own.
Edit: See /u/RFC793's answer below for a much more compact, legible, and Pythonic way of writing this
No reason to produce an intermediate list. You can just use the filter iterator directly and save the memory and time:
regex = r'({\w})' # match {a}, {b}, {c}, etc
result = re.split(regex, tex) # split by regex matches, keeping any matches within a capture group
result = filter(None, result) # remove empty strings from list
result = list(map(lambda el: (el, bool(re.match(regex, el))), result)) # map result to a list of tuples containing the element, and whether or not it is a placeholder
Precompile the expression since you are using it multiple times - faster and more readable. You can do the whole filter/list/map/lambda in a single list comprehension while still being readable as well (that's what they are here for). Of course the tokens intermediate (toks) can be swallowed up by the comprehension too if you are trying to play Python Golf:
regex = re.compile(r'({\w})')
# Split input into tokens based on word boundaries
toks = regex.split(tex)
# Generate list of (token, is placeholder) tuples for all non-empty tokens
result = [(t, bool(regex.match(t))) for t in toks if t]
Empty strings would be returned if there were delimiters at the beginning or end of the string, or if there were delimiters next to one another.
OP’s regex does have a *, so it would match multiple characters inside the brackets. That seems reasonable, but I just went with something that would match the examples they supplied.
keys = re.findall(r'({[^\s{}]*})',tex)
# !!! I should go to fuckin jail for this shit
texcopy = tex # a string containing a latex of the form \int_{a}^{b}
# we want it seperated to \int_ {a} ^ {b} and whether or not the
# substring is a placeholder like {a}, {b}, {z} etc.
largeop = []
for i in keys:
texcopy = texcopy.split(i)
largeop.append((texcopy[0],False))
largeop.append((i, True))
if (len(texcopy)>0):
texcopy = texcopy[1]
if(texcopy is string):
largeop.append((texcopy))
# !!!
52
u/manafount Mar 15 '23 edited Mar 16 '23
Pretty obviously written by somebody who wanted to split a string while keeping the delimiters, stumbled upon this stackoverflow answer, and got a little bit lost along the way :)
Here's how I would break it down instead:
The comments in OP's code contradict their regex, so I simplified it. Obviously they can plug in their own.
Edit: See /u/RFC793's answer below for a much more compact, legible, and Pythonic way of writing this
1
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 16 '23
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!