Is it a way to capture text as 'internal variables' in regex?
Question:
So i have these string test1string2 , test2string2 and test3string3.
Regex "test/dstring/d" would capture the three of them, but is there a way to write a regex that will capture only the second and third strings? Sort of: capture "test" followed by any number followed by "string" followed by a number but only if those numbers are the same.
I am working in python if it adds relevant info
……………………
Answers:
You are looking for backreference in python
‘s re
it is backslash followed by number of capturing group, consider following simple example
import re
texts = ['A1A1','A1B2','A1C1']
for t in texts:
if re.match(r'[A-Z](d)[A-Z]1',t):
print(t)
gives output
A1A1
A1C1
So i have these string test1string2 , test2string2 and test3string3.
Regex "test/dstring/d" would capture the three of them, but is there a way to write a regex that will capture only the second and third strings? Sort of: capture "test" followed by any number followed by "string" followed by a number but only if those numbers are the same.
I am working in python if it adds relevant info
……………………
You are looking for backreference in python
‘s re
it is backslash followed by number of capturing group, consider following simple example
import re
texts = ['A1A1','A1B2','A1C1']
for t in texts:
if re.match(r'[A-Z](d)[A-Z]1',t):
print(t)
gives output
A1A1
A1C1