Reputation: 6899
I am trying to put together REGEX expression to validate the following format:
"XXX/XXX","XXX/XXX","XXX/XXX"
where X
could be either a letter, a number, or dash or underscore. What i got so far is
"(.*?)(\/)(.*?)"(?:,|$)/g
but it does not seem to work
Update: there could be any number of "XXX/XXX" strings, comma-separated, not just 3
Upvotes: 2
Views: 98
Reputation: 2393
you can try the following regex:
"([\w-]+)\/([\w-]+)"
Edit: regex explained:
([\w-]+)
in the square brackets we say we want to match \w
: matches any word character (equal to [a-zA-Z0-9_]
). After this, we have "-
", which just adds literally the symbol "-
" to the matching symbols.+
" says we want one or more symbols from the previous block: [\w-]
\/
matches the symbol "/
" directly. It should be escaped in the regex, that's why it is preceded by "\
"([\w-]+)
exactly like point 1, matches the same thing since the two parts are identical.()
- those brackets mark capturing group, which you can later use in your code to get the value it surrounds and matches.
Example: Full match: 1X-/-XX
Group 1: 1X-
Group 2: -XX
Here is a demo with the matching cases - click. If this doesn't do the trick, let me know in the comments.
Upvotes: 1
Reputation: 27723
Here, we would be starting with a simple expression with quantifiers:
("[A-Za-z0-9_-]+\/[A-Za-z0-9_-]+")(,|$)
where we collect our desired three chars in a char class, followed by slash and at the end we would add an optional ,
.
jex.im visualizes regular expressions:
Upvotes: 0
Reputation: 91373
This will do the job:
"[-\w]+/[-\w]+"(?:,"[-\w]+/[-\w]+")*
Explanation:
" # quote
[-\w]+ # 1 or more hyphen or word character [a-zA-0-9_]
/ # a slash
[-\w]+ # 1 or more hyphen or word character [a-zA-0-9_]
" # quote
(?: # non capture group
, # a comma
" # quote
[-\w]+ # 1 or more hyphen or word character [a-zA-0-9_]
/ # a slash
[-\w]+ # 1 or more hyphen or word character [a-zA-0-9_]
" # quote
)* # end group, may appear 0 or more times
Upvotes: 0