2

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

1
  • Is there something wrong with the provided answers? Commented Jun 6, 2019 at 16:37

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

what about double-quotes?
I didn't understood you need them, edited now, take a look. The link with the example matches is also added. With examples provided from your comments below :) Hope this helps
0

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

Demo

10 Comments

Forgot the double quotes to match.
@sp00m: Good catch, fixed.
I tried this "test/test2","test3/test4","test4/test5" and it failed
oh, forgot to mention that there could be any number of comma-separated strings on type "XXX/XXX"
@CodingDuchess Where is the regex used? Tool/programming language? Remove braces from around *
|
0

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 ,.

Demo

RegEx Circuit

jex.im visualizes regular expressions:

enter image description here

2 Comments

I plugged in to your demo my string - "test/test2","test3/test4","test4/test5" and it showed no matches
yes, could be any number of chanracters but each "Xxxxxxx/xxxxx" string must be separated by commas

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.