Bottom Page

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.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#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') == ''
Quote
#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.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#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.
Quote
#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.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  a module discovering its own name Skaperen 3 61 33 minutes ago
Last Post: Skaperen
  Create database in python module ? Johnse 5 101 Sep-05-2019, 04:14 AM
Last Post: buran
  i need a module for more involved command line parsing Skaperen 21 473 Sep-04-2019, 02:18 AM
Last Post: Skaperen
  want suggested module for e-mail parsing Skaperen 0 93 Jul-26-2019, 08:52 PM
Last Post: Skaperen
  a module for multiple input waiting Skaperen 2 116 Jul-19-2019, 12:26 AM
Last Post: Skaperen
  finding which source files import a module Skaperen 3 333 Apr-22-2019, 09:28 PM
Last Post: Gribouillis
  Has anyone tried the module Black ? Truman 7 573 Apr-17-2019, 02:11 AM
Last Post: Truman
  module to do DNS queries Skaperen 2 418 Jan-01-2019, 08:19 PM
Last Post: Skaperen
  tar module - how usable is it? Skaperen 4 507 Dec-07-2018, 03:44 AM
Last Post: Skaperen
  a85encode vs b85encode (in module base64) Skaperen 0 405 Nov-08-2018, 10:01 PM
Last Post: Skaperen

Forum Jump:


Users browsing this thread: 1 Guest(s)