Help: How do I get the regex for this page number?
example.com/photos/gray?pg=2
And that pg=2
can be any number.
What will the full url look like and what will only the ?pg=X
look like?
The software I need it for has this information: Addresses containing any of the below strings will be downloaded. All of the below strings are treated as regex
I have tried \?pg=.*
and it didn't work.
What am I missing?
CodePudding user response:
The reason your regex did not work is that you did not escape the ?
character with a backslash. In regex, the ?
is also used for a lazy selection.
A working regex would be:
\?pg=([0-9]*)
But for parsing URLs it is not recommended to use regex and an URL parser should be used instead.
CodePudding user response:
- If you want to get a string like ?pg=X then you can use
\?pg=\d*
.
\d*
- matches any number.
- If you want to get only page number (an integer), you can use this regex:
(?<=pg=)\d*
(?<=pg=)
is a positive lookbehind, it searches everything after the string pg=.\d*
- matches any number.
You didn't mention a flavour for your regex, but positive lookbehind construction works in all given flavours at regex101.com.