Joe Z Joe Z - 6 months ago 21
HTML Question

Regex Select numeric string between elements

I have been searching stack and Google and have figured out some of what I'm trying to do. Here is the string

?w=2796&


I need to get the numerics between
w=
and
&


So it should return
w=2796


Here's what I have so far:
w=(.*?)&


This does work but is returning the
&


Is there a way to to return only
w=numeric


This regex is also returning some other elements which I don't need in the HTML in some instances.

Answer

It's going to depend on which regex engine you use. & may have special meaning. I think the simplest approach here is something like /w=(\d+)/ or /w=([0-9]+)/ assuming no negatives or decimals in your numbers

Comments