regular expression query
Sorry about the rather unspecific title.
A string can assume the following valid values: "(a, b)" "(a)" "(b)"Any ideas how I can express this disallowing empty brackets "()"? (I am using Python.) |
If your only goal is to disallow empty brackets:
Code:
\([ab ,]+\) Code:
\((a, b|a|b)\) |
Alucard's answer is exactly what I would do - the only thing I would add is that if you're using python, you'll need to either provide the regex as a raw string, or escape the '\'s. So, either
Code:
regex = re.compile(r"\((a, b|a|b)\)") Code:
regex = re.compile("\\((a, b|a|b)\\)") |
Quote:
Code:
#a (10-30, 4-5pa) And it will get really nasty once I try to extract the numbers, which is the ultimate objective. |
I think this one does it without repitition of "a" and "b":
Code:
regex = re.compile(r'(\(a(,|\)))?((\(| )b\))?\)?') |
You can also use python's string formatting to make your expressions a little more readable:
Code:
a = "Some RE string" |
Quote:
Quote:
Code:
re_element = r'(?:(?:%s)(?: |\Z))?' Code:
regex = re.compile(r'\(m?(, )?n?\)') Just ran this monster over 340,000 records and it does the trick. |
All times are GMT -5. The time now is 03:11 AM. |