3

I have following test strings:

  1. http://example.com/?foo=bar&path=a/b/c.png/&bar=foo
  2. http://example.com/?foo=bar&path=a/b/c.png&bar=foo
  3. http://example.com/?foo=bar&path=a/b/c.png
  4. http://example.com/?foo=bar&path=a/b/c.png/

I'm looking for a regex that only matches path=a/b/c.png(the path query parameter without the trailing slash). The path query parameter can be in the beginning, middle or end of the row.

So far I have following regex path=[^#&]*:

You can see it here: https://regex101.com/r/v4DqDx/1/

Thanks for any help!

EDIT: To be more clear, the query parameter in row 1 and 4 with the trailing slash should not match.

0

1 Answer 1

2

Code

See regex in use here: Uses m flag for multiline

\bpath=[^#&\n]*\.\w+(?=[&#]|$)

If the content is not multiline use this instead:

\bpath=[^#&]*\.\w+(?=[&#]|$)

Explanation

  • \b Assert position as a word boundary. A word boundary is a location that matches (^\w|\w$|\W\w|\w\W)
  • path= Match this literally
  • [^#&\n]* Match any character except #&\n any number of times
  • \. Matches a literal dot
  • \w+ Matches one or more word characters
  • (?=[&#]|$) Positive lookahead ensuring either a character in the set &# matches or it's the end of the line.
Sign up to request clarification or add additional context in comments.

6 Comments

@RomanPerekhrest did you even read my answer? I specifically put a note for this. I include a link to regex101. This 100% works.
@RomanPerekhrest edited to prevent further confusion. If you wouldn't mind cleaning up your comments after too (to prevent confusion for future viewers)
Thanks for your help but I'm looking for a regex that doesn't match the query parameter in row 1 and 4 (ignores the query parameter with a trailing slash).
@Koji maybe that should've been specified in the question then give me a moment
@RomanPerekhrest you're missing the m flag. Why don't you just click on the regex101 link I created to see it in use instead of trying to get it to work yourself?
|

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.