skip to Main Content

I have a string, and I want to define if it a correct pathname and that at least one (/:)|(^/?:) pair of symbols exists and that all in one step.

Correct pathname must include next symbols [a-zA-Z0-9.~!$&'()*+,;=@-_s:] in each portion, divided by ‘/’.

For example:

  1. :param1/path1/:param2 – correct, with defining groups {param1, param2}
  2. path1/path2/path3 – incorrect
  3. path1/:param1 – correct, with defining group {param1}

Is it possible to do in one step with a help of regular expression?

2

Answers


  1. I’d probably do it with this regex :([^/]+)

    https://regexr.com/7prjd

    Login or Signup to reply.
  2. have a try:

    (?<=^/?:|/:)(w+)
    

    https://regex101.com/r/8U3oiw/1

    maybe this:

    ^(?=.*?/?:.*?)(?=.*?/:.*?)(?:(?::(w+))?.*?/:(w+))
    

    https://regex101.com/r/Ennltp/1

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search