#πŸ”’ regex string matching

71 messages Β· Page 1 of 1 (latest)

fast raptor
#

I need to match these strings in this file
I was thinking something like match = re.search("([A-H][LR][01] ){9}[A-H][LR][01]", line), but I was wondering if there was a better way

teal tangleBOT
#

@fast raptor

Python help channel opened

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.

frosty crescent
#

or instead of *, put {9} to match 9 more of those

#

also did you mean to use .search or .findall?

fast raptor
#

does \1 mean the same thing as the first capturing group, i.e, [A-H][LR][01] ?

fast raptor
fast raptor
#
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)
frosty crescent
#

python uses \X to refer to group X in the regex

frosty crescent
#

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])

teal tangleBOT
fast raptor
frosty crescent
#

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

frosty crescent
fast raptor
frosty crescent
#

but with {9} instead of * for a fixed length

fast raptor
#

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)

nova void
#

why not just r"( \w{3}){10}"

#

is it really that important that you make it so extraordinarily specific?

nova void
#

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)

fast raptor
#

the ending suffix may be different, but they all end with a comma

nova void
#

gotcha, then just makeTM (.*),

#

don't make it more specific than you have to

fast raptor
#

and then I access the match using match.group(1) ?

nova void
#

yep

fast raptor
#

(I'm a newbie)

nova void
#

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

fast raptor
#

oh that's a good solution, thanks

nova void
#

np

pallid trellis
#

or makeTM ([^,]+)

nova void
#

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
fast raptor
#

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)
nova void
#

or ([^,]*) or ([^,]+) if you prefer that for some reason

pallid trellis
nova void
fast raptor
nova void
#

* 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

fast raptor
#

I tried it without the question mark and it seemed to work

nova void
#

great, whatever then

#

must be because of the line breaks

#

if you had two matches on one line it wouldn't work

fast raptor
#

what if I just specified the exact number of characters instead of *

nova void
#

that would also succeed its just more work

pallid trellis
#

then you can just slice the string for each line

#

no regexp required

teal tangleBOT
fast raptor
#

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)
pallid trellis
fast raptor
#

I think this is solved now, thanks for the help everyone

#

!close

teal tangleBOT
#
Python help channel closed with !close

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.