Kriskris Kris
Kriskris Kris

Reputation: 1

regex, repeating part of expression

I wonder how can I improve my expresssion to check everything in one expression instead of create a lot of rules.

here is my expressions

/from-([^/]+)-([^/]+)/
/from-([^/]+)-([^/]+-[^/]+)/
/from-([^/]+)-([^/]+-[^/]+-[^/]+)/

I would like check phrase: from-Country-city or from-Country-city-city or from-Country-city-city-city

Upvotes: 0

Views: 63

Answers (1)

Zach Victor
Zach Victor

Reputation: 469

I believe that this expression satisfies your requirements:

from-(\w+)-([\w-]+)

Here is a live example/demomstration: https://regex101.com/r/gF0fS2/1

The meaning of the expression:

  1. Match the literal string from-
  2. followed by one or more word characters, defined as group 1 (\w+)
  3. followed by the literal string -
  4. followed by one or more of the set of word characters or the dash, defined as group 2 ([\w-]+)

For more information on regular expressions, please see these websites:

Upvotes: 1

Related Questions