tags:

views:

53

answers:

1

I'm trying to use Regex to get a bit if HTTP header parsing done. I'd like to use groups to organize some of the information:

Let's say I have this:

Content-Disposition: form-data; name="item1"

I'd like the result of my regex to create two groups:

contentdisposition : form-data
name : item1

I've tried several methods, but I can't seem to figure out how to do this. If name= doesn't exist then only one group should be created, but the regex should not error out.

Any ideas?

+2  A: 

/Content-Disposition: (.*?);(?: name="(.*?)")?/ might be what you're looking for. It uses an optional greedy quantifier to get the name unless that would cause the match to fail.

burningstar4