RegEx with literal '='

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
AgentSmithers
Posts: 8
Joined: Fri Feb 17, 2017 6:23 pm

RegEx with literal '='

Post by AgentSmithers » Sun Feb 19, 2017 2:20 am

Hi Everyone,
In URE, Does anyone know how to escape the '=' in the regEx? I cant seem to isolate the GET var's without looking for the '=' in the middle.
Thank you!!

Code: Select all

import ure
MyString = "GET http://test.ing/test.html?Test=Value&another=one&here=two HTTP/1.1"
reg = ure.match("([A-Z]+)", MyString)
variables = ure.match("([A-z]+)[\=]([A-z]+)", MyString)
print(reg)

i=0
while 1:
	try:
		if reg is None:
			break
		else:
			print(reg.group(i))
			i+=1
	except IndexError as e:
		#print("Index Exception: {0}".format(e), type(e))
		break
	except Exception as e:
		print("Exception: {0}".format(e), type(e))
		break

print(variables)
i=0
while 1:
	try:

		if variables is None:
			break
		else:
			print(variables.group(i))
			i+=1
	except AttributeError as e:
		print("Attribute Exception: {0}".format(e), type(e))
		break
	except IndexError as e:
		#print("Index Exception: {0}".format(e), type(e))
		break
	except Exception as e:
		print("Exception: {0}".format(e), type(e))
		break

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: RegEx with literal '='

Post by Roberthh » Sun Feb 19, 2017 8:00 am

Code: Select all

variables = ure.search("([A-z]+)=([A-z]+)", MyString)
will deliver at least the first assignment, test=value.
with

Code: Select all

variables.group(0) -> "Test=Value"
variables.group(1) -> "Test"
variables.group(2) -> "Value"
ure.match is anchored. It will only find patterns at the start of the string. If you want to find all occurrences of that xxx=yyyy, you have to iterate trough your source string.

AgentSmithers
Posts: 8
Joined: Fri Feb 17, 2017 6:23 pm

Re: RegEx with literal '='

Post by AgentSmithers » Sun Feb 19, 2017 8:33 am

Darn, Thank you for the reply sir!

Post Reply