#π regex string matching
71 messages Β· Page 1 of 1 (latest)
@fast raptor
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
why not just ([A-H][LR][01])( \1)*?
or instead of *, put {9} to match 9 more of those
also did you mean to use .search or .findall?
does \1 mean the same thing as the first capturing group, i.e, [A-H][LR][01] ?
I want it to return the match as a string, like this
"BR0 HR1 CR1 DL0 DR1 ER0 BL1 DL0 ER1 AR1"
yes
import re
input_file_path = "/content/Decider_Halt_Hardcoded_Parameters.v.txt"
output_file_path = "output.txt"
with open(input_file_path, "r") as input_file, open(output_file_path, "w") as output_file:
for line in input_file:
match = re.search("([A-H][LR][01] ){9}[A-H][LR][01]", line)
python uses \X to refer to group X in the regex
if you want the whole string then i cant really remember if there's a way on the match object to do it
i just do .span(), unpack it, then slice .string
!e
import re
m = re.search('world', "hello world")
s, e = m.span()
print(m.string[s:e])
:white_check_mark: Your 3.12 eval job has completed with return code 0.
world
I guess I could also do findall for each of the individual 3 letter parts, and then join them with space?
thats probably a bad idea
just use finditer and build the matched string yourself
if you want, you can wrap it in another layer of brackets and then get group 1
i think this might be best for you
okay
but with {9} instead of * for a fixed length
am I doing this correctly?
import re
input_file_path = "/content/Decider_Halt_Hardcoded_Parameters.v.txt"
output_file_path = "output.txt"
with open(input_file_path, "r") as input_file, open(output_file_path, "w") as output_file:
for line in input_file:
match = re.search("([A-H][LR][01])( \1){9}", line)
if match:
print(match.group())
I wonder if I can just go with match = re.search("[A-H][LR][01] [A-H][LR][01] [A-H][LR][01] [A-H][LR][01] [A-H][LR][01] [A-H][LR][01] [A-H][LR][01] [A-H][LR][01] [A-H][LR][01] [A-H][LR][01]", line)
why not just r"( \w{3}){10}"
is it really that important that you make it so extraordinarily specific?
true
you can also do makeTM (.*),Ha
like that seems good enough
and you can just do re.findall with input_file.read() unless the file is massive (more than 100 mb)
the ending suffix may be different, but they all end with a comma
and then I access the match using match.group(1) ?
yep
(I'm a newbie)
with re.findall it will only return whats in the group
so this is gonna work how you want it to ```py
for group in re.findall(r"makeTM (.*),", input_file.read()):
print(group.split()) # prints ["BR1", "ER0", "CL1", "AR0", ...] for each line
oh that's a good solution, thanks
np
or makeTM ([^,]+)
good point I should mention that you actually have to do r"makeTM (.*?),"
the lazy grabbing is better than [^,] but it is still required
otherwise .* will be too greedy and still gobble up the commas
so
for group in re.findall(r"makeTM (.*?),", input_file.read()):
print(group.split()) # prints ["BR1", "ER0", "CL1", "AR0", ...] for each line
so could I just do this?
with open(input_file_path, "r") as input_file, open(output_file_path, "w") as output_file:
matches = re.findall(r"makeTM (.*),", input_file.read())
print(matches)
yes but update the pattern to have (.*?) instead of (.*)
or ([^,]*) or ([^,]+) if you prefer that for some reason
how is it better (objectively)?
intent is clearer and its fewer characters
what does the "?" do?
* by default is very greedy and it just keeps matching stuff until it can't anymore
*? means that it will stop matching as soon as it can (it's lazy)
so .*?, means that it's gonna stop matching as soon as it finds a comma
whereas in .*, the star will just continue matching forever even if there is a comma
I tried it without the question mark and it seemed to work
great, whatever then
must be because of the line breaks
if you had two matches on one line it wouldn't work
what if I just specified the exact number of characters instead of *
that would also succeed its just more work
not all the lines have matches though
Click here to see this code in our pastebin.
this seems to work perfectly
with open(input_file_path, "r") as input_file, open(output_file_path, "w") as output_file:
matches = re.findall(r"makeTM (.{39}),", input_file.read())
print(matches)
okay, then it doesn't work
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.