2

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?

1
  • 1
    Simple parsing could be more suited for the given situation, headers are usually pretty predictable. If you want to go the regex route, please add a tag of the language you're using, you'll get a lot more answers that way. Commented Aug 2, 2010 at 17:14

1 Answer 1

3

/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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.