Python Forum
re module: want a regexhttps://python-forum.io/Thread-re-module-want-a-regex
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
re module: want a regexhttps://python-forum.io/Thread-re-module-want-a-regex
#1
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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') == ''
Reply
#3
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
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.
Reply
#5
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  importing a module given a path to the file Skaperen 8 1,198 Nov-04-2023, 07:03 PM
Last Post: Skaperen
  where module is loaded from Skaperen 1 710 Jul-26-2023, 05:38 AM
Last Post: Gribouillis
  looking fo documentation for module files Skaperen 39 79,441 Aug-09-2022, 07:37 AM
Last Post: Gribouillis
  Mixed types of numeric data in the statistics module stevendaprano 2 1,269 May-23-2022, 02:15 AM
Last Post: stevendaprano
  a module can be found but another cannot Skaperen 7 2,631 Oct-25-2021, 12:16 AM
Last Post: Skaperen
  Eric6 No module named 'serial' BvdP4Py 0 2,688 Jul-30-2021, 08:38 AM
Last Post: BvdP4Py
Lightbulb Feedback needed! Controlled entropy in programs with 'ordered' module grandrew 0 1,395 Jul-27-2021, 01:49 AM
Last Post: grandrew
  PIL module and Python 2.7 under Windows 10 Lad 3 2,105 Jul-06-2021, 07:39 PM
Last Post: Lad
  visual studio code python how to get source code of import module definitian umen 2 3,865 Jun-13-2020, 06:20 PM
Last Post: umen
  finding a mismatched triple-quote in a huge module Skaperen 1 1,535 Apr-15-2020, 04:29 AM
Last Post: UGuntupalli

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020