Reputation: 21
I have this code that I'm trying to get to work with Regex but I'm new to this and I'm not sure what I'm doing wrong.
I'm trying to get anything that is between paralash
and ?trk
now that will not work because of the ?
. If I remove the ?
out the code then I get the numbers with ?
and that is not what I need. I only need the numbers. Any help
/paralash/1229857?trk=vsrp_companies_res_name
(?<=paralash/).*(?=?trk=) ---- I get nothing
(?<=paralash/).*(?=trk=) ----- I get 1229857? <-- I don't want the ?
How can I fix it?
Upvotes: 2
Views: 106
Reputation: 38416
You have a few ways to go here, some better than others but all depending on your needs.
The first, using your current regex, is to simply escape the ?
which has a special meaning in regex of "may or may not exist":
(?<=paralash/).*(?=\?trk=)
However, you can improve this by changing the greedy match into a "digits only" match by using \d+
:
(?<=paralash/)\d+(?=\?trk=)
Also, if you're doing groupings and not just matching you can remove the lookbehind and make it a prefix instead:
paraslash/(\d+)\?trk=
Upvotes: 1
Reputation: 6333
i think it's perl compatible syntax.
it's better to group what you want instead of looking backwards and forwards:
\/paralash\/(\d+)\?trk=
then use \1 or $1 to capture the result.
i escaped /
because i assumed /
is your delimiter.
looking backwards and forwards is a tradeoff that slower down the regex but gain more power. in your case, it could speed up without them.
Upvotes: 0
Reputation: 11116
use this:
(?<=paralash\/).*(?=\?trk=)
demo here : http://regex101.com/r/kK2nC1/1
Upvotes: 0