Python Forum
re module: want a regexhttps://python-forum.io/Thread-re-module-want-a-regex - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: re module: want a regexhttps://python-forum.io/Thread-re-module-want-a-regex (/thread-21106.html)



re module: want a regexhttps://python-forum.io/Thread-re-module-want-a-regex - Skaperen - Sep-14-2019

a script i am working on will get a list of parameters to parse into parts. i am hoping re can do this for me but i am clueless how to make an appropriate regex. i can barely filter out .py files from a list of files.

each parameter is 3 decimal numbers each preceded with one of the characters "/" "@" or "+". the character in front identifies which number it is. for example, the number that follows "@" is the workspace number. for each parameter i need to get the 3 numbers (as 3 strings) identified with which character in front. for example i need to get the same results for "@1/2+3", "/2@1+3", "@1+3/2", "+3@1/2", "/2+3@1", and "+3/2@1". so it's more than just matching the pattern. it' associating various parts together in the right order. i think the match has to have a way to give a choice of any of the 3, each expressed with its leading character and which index it goes to, and do exactly 3 of them. but i don't know how to express this or how to be sure "@1@2/3" does not match (because "+" is missing). regex is a whole other language, harder than even lisp.


RE: re module: want a regex - Gribouillis - Sep-14-2019

I would start with this
>>> pat = re.compile(r'([@/+]\d+(?:[.]\d*)?)')
>>> pat.findall('/2@3+4')
['/2', '@3', '+4']
I don't think it's a good idea to look for a one liner here. You can check that the list has 3 elements, you can convert the list into a dictionary {'@': '3', '/': '2', '+': '4'} and check again that the dict has 3 elements. Finally you can check that the string contains nothing else with pat.sub('', '/2@3+4') == ''


RE: re module: want a regex - Skaperen - Sep-14-2019

is there at least a way to test if the string is at least valid (has exactly one of each and has all three in any order) without getting the values. it's the test i want to in one line so i can put it in a if/elif stack. if not then i guess i have to put the test in a function.


RE: re module: want a regex - ndc85430 - Sep-14-2019

re.match returns a match object or None if the string doesn't match the pattern.

I don't think you should obsess about doing things in a single line.


RE: re module: want a regex ... - Skaperen - Sep-19-2019

doing conditionals in one line or at least one very long line is needed when doing a stack of elifs. of course you can generally encapsulate the test into a function and call it in one line. i just wish there was an easy way to code the actual test in one line, pass that to the function, and have the function do it ... inside a try/except.

that, or add something like the foo = expression1 if condition else expression2 to do try/except and return on expression or the other like foo = try expression1 except whatever expression2.