2

I have the following input text:

pagelimit=50&filtercolumn=Datacenter&filtervalue=abfg1&filtercolumn=MachineType&filtervalue=fg&filtercolumn=GPG&filtervalue=IPMI

I want to get back

  1. filtercolumn=Datacenter&filtervalue=abfg1
  2. filtercolumn=MachineType&filtervalue=fg
  3. filtercolumn=GPG&filtervalue=IPMI

There may be an unlimited amount of these.

I have tried a few things. I'm currently trying something like this:

(?:((filtercolumn=.*&filtervalue=.*)+)?)

But of course it doesn't work. I get:

  1. filtercolumn=Datacenter&filtervalue=abfg1&filtercolumn=MachineType&filtervalue=fg&filtercolumn=GPG&filtervalue=IPMI
  2. filtercolumn=Datacenter&filtervalue=abfg1&filtercolumn=MachineType&filtervalue=fg&filtercolumn=GPG&filtervalue=IPMI
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
Manther
  • 41
  • 5
  • check this post for using regex in Go to split strings. https://stackoverflow.com/questions/4466091/split-string-using-regular-expression-in-go – Keith Aymar Jul 26 '18 at 19:43
  • 2
    That string looks suspiciously like URL query parameters (the stuff after `http://.../path?`) and if it is then [(net/url).ParseQuery](https://godoc.org/net/url#ParseQuery) might be a useful tool. – David Maze Jul 27 '18 at 00:35

1 Answers1

7

Try this \bfiltercolumn=[^&]*&filtervalue=[^&]*

https://regex101.com/r/sasmXL/2

LarsH
  • 27,481
  • 8
  • 94
  • 152
  • Definitely works in that site. Now trying to try it out in go. What is the \b doing? – Manther Jul 26 '18 at 19:46
  • 1
    It's just a word boundary, it prevents matching something like `autofiltercolumn=...&filtervalue=...` –  Jul 26 '18 at 19:48
  • Probably doing something dumb. but I have `a := regexp.MustCompile("\bfiltercolumn=[^&]*&[^&]*") var c = a.Split(r.URL.RawQuery, -1) ` returns a single item array of: `pagelimit=50&filtercolumn=Datacenter&filtervalue=vin1&filtercolumn=MachineType&filtervalue=sd&filtercolumn=APC&filtervalue=IPMI ` – Manther Jul 26 '18 at 19:55
  • 1
    I don't use go lang so I can't help you with regex usage. If the regex works on regex101 under the _golang_ tag, it definitely works in code. I would check to make sure it's a global match and that the `"\b"` doesn't need to be escaped `"\\b"` as well. –  Jul 26 '18 at 20:01
  • 1
    I just needed to use FindAllString instead of split. Thanks for the help. – Manther Jul 26 '18 at 20:29