Reputation: 27
I am working on regular expressions.
id: 1 food: apple, banana
id: 2 food: orange
ids: 3 food: apple, banana
id: 4 food: hello, yellow
id: 5food: apple, banana
id:6 food: ,,,yellow
This is my regular expression code:
pattern = /id[:] [[:digit:]]+ food[:] [a-z,]+/
id: 1 food: apple, banana
id: 2 food: orange
id: 4 food: hello, yellow
id: 6 food:,,,yellow
This expression is able to invalid everything except the last line. The last line in the list should not be printed. How can detect that the something doesn't start with ,,,
Edit: There is only one space allowed
Upvotes: 0
Views: 61
Reputation: 110755
I understand that multiple contiguous spaces are not permitted.
r = /
^ # match beginning of line
id:\s # match "id:" followed by a space
\d+\s # match > 0 digits followed by a space
food:\s # match "food:" followed by a space
[[:alpha:]]+ # match > 0 letters
(?:,\s[[:alpha:]]+) # match comma, space, > 0 letters in a non-capture group
* # perform match on above non-capture group >= 0 times
$ # match end of string
/x # free-spacing regex definition mode
str =<<_
id: 1 food: apple, banana
id: 2 food: orange
ids: 3 food: apple, banana
id: 4 food: hello, yellow
id: 5food: apple, banana
id:6 food: ,,,yellow
_
str.scan(r)
#=> ["id: 1 food: apple, banana",
# "id: 2 food: orange",
# "id: 4 food: hello, yellow"]
Upvotes: 1
Reputation: 121010
▶ input.scan /^id:\s+\d+\s+food:\s+(?:[a-z]+(?:,\s)?)+$/
#⇒ [
# [0] "id: 1 food: apple, banana",
# [1] "id: 2 food: orange",
# [2] "id: 4 food: hello, yellow"
# ]
Upvotes: 1