+ 2
Python regex to match pattern of number and space or comma
Hi I want to write a regex to allow below scenarios: One or more number separated by space or comma. For example, "12 22" is valid and "02 22 " is also valid and "12,22" is also valid but "12,22 22" is not valid as it combines both space and comma. Also , there can be any positive number separated by either space or comma. r"^[1-9]?[\s|,]?" seems not enough and not working. Can anyone help me to proceed further?
28 ответов
+ 2
My suggestion would have been
/^\d+((,\d+)*|( \d+)*)$/gm
+ 5
Ketan Lalcheta
just use
pattern = r"^\d+((,\d+)*|( \d+)*)quot;
there are slight variations in the regex syntax depending on the programming language. The /...../gm was used by regex101.com. I think it's javascript...
g for global match find all matches
m for multiline matching. Because the tests below are multi lines.
+ 3
Mirielle
"Some people, when confronted with a problem, think 'I know, I'll use regular expressions." Now they have two problems"
--Jaime Zawinski
Regex have their uses, and being able to write efficient regex is a skill. Compare my reply and Gordie's concise regex. 😅 Mine is redundant and slow.
But I agree it should be a a last ditch option. They are notoriously hard to read for most people...
Wong Hei Ming
Validation is a general term. Maybe the regex is used as part of a match verifier function. If there is a match, then it returns True, else False.
+ 1
I'm not good at regular expression.
But if you have two patterns to match, isn't it better to construct two separate regex?
And, what would you like to do with "123abc"?
It contains digit, but no space or comma. Do you want "123" get returned too?
+ 1
To clarify your requirement, what are you expecting if the string is "12, 22 22" and "123abc"?
+ 1
how about
/((^\d+((\s)\d+)*$)|(^\d+(,)\d+)*$)/gm
the first two should be selected, but not the last two.
12,34,56
12 34 56
12,34 56
12 34,56
https://regex101.com/r/vLOwUp/3
+ 1
Gordie
yes, that's the proper way.
The steps are also way less than my regex.
I just used the lazier method and broke the problem into two sections...😁
factoring out the middle branch is the step I missed...
regex rules are so hard to remember...
+ 1
Yes, i noticed that Bob_Li. But my solution also has two branches, they just split after the initial digits :)
+ 1
In general speaking when someone say "validation" in programming, I am expecting a True or False value.
However regular expression, here in Python, return a matching object or None if the pattern is not found.
Since you keep talking "valid" and "invalid", then we need to know "how" you determine it.
You said "One or more number separated by space or comma" and gave "12 22", "02 22", "12,22" and "12,22 22" as examples.
We need to ask "if it contains comma, is it allowed to have multiple commas like 12,23,34? Or you consider it is wrong because it has 2 commas?
That's why I keep asking different scenarios because your goal is not clear to me.
Someone asked a question months back it was like a simple at first but it turns out the real question is very different. It took me and other member spent days and multiple pages to clarify the question.
And I don't want to do it again.
Finally and yes, it is possible to use regular expression to do "validation" and here is one of the exercises. (Strong Password Detection)
https://automatetheboringstuff.com/2e/chapter7#calibre_link-273
I created 3 regex to do the job.
If your question is your homework, I'm not knowledgeable to help and you can keep asking.
But if it is a personal project, it has a easier way to do and not worth to take a hard way.
+ 1
Bob_Li and Gordie ,
I tried but I believe I am missing something as it is not working in below code :
https://sololearn.com/compiler-playground/c082gKFYcZBB/?ref=app
Also , could you please help me understand regex you people shared?
r"/^\d+((,\d+)*|( \d+)*)$/gm"
I know first part is allowing digit with comma and second part is for digit with space.
\d is digit
* means one or more instances
^ means start and $ means end
What does gm denote here ?
+ 1
Great Bob_Li . Many thanks to one and all who responded. I appreciate all response and do understand your concern as well as suggestions.
+ 1
Ketan Lalcheta
you should mark Gordie as best answer.
+ 1
Sure. Done.
+ 1
I want an inheritance program in c++ contain
Class student
Name ,roll no,stream ,optional subject ,medical , engineering.
0
123 is valid but 123abc is false. I think two patterns should not be used but that is what I understand
0
Wong Hei Ming both invalid as per his requirement
0
For fist query:
"12,22 22 " is not valid. As it has both space and comma.
"12 22 22" or "12,22,22" both are valid.
Second query:
"123abc" not valid as it has characters. Only digits are allowed.
0
To describe more.
Allowed characters are digits (0 to 9) space and comma.
If space is available, it should be space only. If comma is available, only comma is allowed then. Mixing both are not allowed.
Also 12 alone is also valid.
0
Maybe you use regular expression wrongly.
Regular expression is to return a match pattern from a string, not to "validate" the string.
For example:
regex = re.compile(r'\d+')
string = '12, 22 22'
mo = regex.findall(string) # ['12', '22', '22']
It searches for a pattern which contains at least 1 digit, so 12, 22 and 22 are returned.
regex2 = re.compile(r'\d+,')
string = '12, 22 22'
mo = regex.findall(string) # ['12,']
It searches for a pattern which contains at least 1 digit with a comma at back, so "12," is returned.
regex3 = re.compile(r'\d+ ')
string = '12, 22 22'
mo = regex.findall(string) # ['22 ']
It searches for a pattern which contains at least 1 digit with a space at back, so "12," is returned.
regex4 = re.compile(r'\d+')
string = 'abcdef'
mo = regex.findall(string) # [] (an empty list)
Because the string doesn't contain any digit, findall return an empty list.
If you use mo = regex.search(string), it returns None.
In your case you are checking the string is valid or invalid, using if statement make more sense.
String = '12, 22, 22'
found = dict()
for s in string:
if s not in found:
found[s] = 1
else:
found[s] = found[s] + 1
if found.get(',', 0) >= 1 and found.get(' ', 0) >= 1:
print(False)
else:
print(True)
0
Thanks Wong Hei Ming
If statement are always there but regex is more powerful.
Also it does help us to find substring and do input validation as well mentioned in below article :
https://www.sololearn.com/learn/9704/?ref=app