1

I'd like to use a regular expression function to remove an item from a querystring based on the key.

Say I have a querystring like this:

page.asp?item1=data1&item2=data2&item3=data3

I would like to call a function like this:

newQs = removeQsItem(url, "item2")

...and have that function return this:

page.asp?item1=data1&item3=data3

... but I also need it to work if called with item 1 or 3 - regardless of their position in the querystring.

My tactic so far it to try and grab "requiredItem={anything but ampersand here}". But I'm really struggling with regex syntax and my brain is about to melt.

Any help would be greatly appreciated.

Martin Hansen Lennox
  • 2,837
  • 2
  • 23
  • 64
  • IS there any way you're using RegEx for this? (or using asp-classic for that matter at all?). Have you seen http://stackoverflow.com/questions/529551/how-can-i-remove-item-from-querystring-in-asp-net-using-c?rq=1 – Benjamin Gruenbaum May 03 '13 at 23:38
  • I just seems like a useful regex function that I'd like to have in my armoury. – Martin Hansen Lennox May 04 '13 at 16:12

2 Answers2

0

Does it have to be with a RegEx? Can you just do some string functions like getting the StrPos of the given string "item2" then finding the StrPos of the next ampersand then just SubString those positions out to get "item2=data2&" (you may have to add 1 to the index of that second postion to actually get the ampersand?) the just do a myQueryString.Replace(substringToRemove, "") to replace that substring with nothing?

Josh P
  • 1,325
  • 14
  • 12
0

I'm not an expert at regular expressions, but I have been trying to learn. So I did some research (and trial & error), and came up with this. Someone else can probably make this cleaner.

(\bitem2=[A-Za-z0-9]*&|&\bitem2=[A-Za-z0-9]*$|^\bitem2=[A-Za-z0-9]*$)

I'll explain it a bit. First, there's 3 searches in one.

  • The first one finds your item IF it's followed by a ampersand & (so we can remove the trailing &)
  • The second one finds your item IF it's the last item (so we can remove the preceding &)
  • The last one is if your item is the ONLY item.

The code:

  • ( this | that ) means OR. Match this OR that.
  • \b matches a word boundery. So we know we're matching a whole word, and not just part of one.
  • [A-Za-z0-9]* matches zero or more letters or numbers. Zero in case you have an empty value.
  • $ matches the end of line
  • ^ matches the start of line
Tom Collins
  • 4,069
  • 2
  • 20
  • 36
  • Thanks Tom, that's pretty sweet. You know it doesn't seem to pick up the item if it's the only one, unless I add an ampersand to it. I don't understand that because surely (^\bitem2=[A-Za-z0-9]*$) should match item2=[zeroOrMoreAlphanumericCharacters] ? That is, there's no mention of the ampersand at the end of that expression. – Martin Hansen Lennox May 04 '13 at 16:02
  • Also, there may be some funky characters coming through in the querystring, not just alphanumerics. So I'd also like a way to match "any character except &". I posted that as a separate question here: http://stackoverflow.com/questions/16376284/how-do-i-match-any-character-except-x-in-a-regular-expression – Martin Hansen Lennox May 04 '13 at 16:11
  • Eventually I used [^&]* to match anything except an ampersand. – Martin Hansen Lennox May 04 '13 at 16:56
  • Not sure why it didn't pick up a single parameter. It worked on the regex test site I used. But glad you got it working. – Tom Collins May 05 '13 at 15:07