Reputation: 2841
I am trying to create a regular expression that will work for the following:
GET://HOSTNAME1234:14159?sim=C:\sim.zip&bif=C:\bif.txt&wpt=C:\wpt.txt
Or more simply:
GET :// HOSTNAME1234 : 14159 ? sim = C:\sim.zip & bif = C:\bif.txt & wpt = C:\wpt.txt
With this:
(\w+)://(\w+)(:(\d+)\??)?((\w+)=([\/\.\\:\w]+)&?)*
What I'm hoping to get out is something like this:
0: [0,72] GET://HOSTNAME1234:14159?arg0=C:\sim.zip&arg1=C:\bif.txt&arg2=C:\wpt.txt
1: [0,3] GET
2: [6,18] HOSTNAME1234
3: [18,24] :14159
4: [19,24] 14159
5: [57,72] arg2=C:\wpt.txt
6: [57,61] arg2
7: [62,72] C:\wpt.txt
Where 6 and 7 represent what I want, however I am not seeing "arg0" or "arg1". I assume I am doing something wrong with the +
and *
commands, but no combination of arrangements seem to work.
I am using this tool to help me: http://www.regexplanet.com/advanced/java/index.html / http://fiddle.re/h9b88
Upvotes: 0
Views: 265
Reputation: 73024
String to match:
GET://HOSTNAME1234:14159?arg0=C:\sim.zip&arg1=C:\bif.txt&arg2=C:\wpt.txt
Despite that you REALLY should be using a URL parser to do this:
Updated Method (works on full original string)
((\w+):\/\/(\w+):(\d+)\??|([^&]*?)=([^&]*)?)
Working example: http://regex101.com/r/pL2wV9
First Method (works, but requires a loop)
Break the URL up into two parts:
Use (\w+):\/\/(\w+):(\d+)\?(.*$)
to break the URL up into: http://regex101.com/r/pQ1vS9
MATCH 1
GET
HOSTNAME1234
14159
arg0=C:\sim.zip&arg1=C:\bif.txt&arg2=C:\wpt.txt
And then on group 4, use ([^&]*?)(?==)=?([^&]*)?
to match the query string once you've captured it, and iterate through it: http://regex101.com/r/bX3uM4
MATCH 1
arg0
C:\sim.zip
MATCH 2
arg1
C:\bif.txt
MATCH 3
arg2
C:\wpt.txt
Disclaimer: This is a really bad idea, and while I wanted to know if I could pull it off, using the URL parser is a much faster, much more accurate route.
Upvotes: 1