1

I'm working on a REST API. The client is using the Accept header in their request to send in stuff like

  • ...application/vnd.mywebsite+json; version=1... or
  • ...application/vnd.mywebsite+xml; version=2....

Currently, I am parsing the headers and picking out the media type and version to serve with string functions:

  • json and 1
  • xml and 2

I was wondering if I could do that faster with a regex.

How can I pull out the format and version from an "Accept" header in the request? I suppose, I would need to make 2 regex calls to get this done, and that's okay.

Update :

Using the answer below, I tried extracting those using ColdFusion, but the pattern just matches the whole string.

Ideally, I want an array of 2 elements, ie ['json', '1']. Any ideas ?

<cfscript>
    arrTitles = reMatch(
        "application/vnd.website\+([A-Za-z]+);\s*version=(\d+)",
        "application/vnd.website+json; version=2"
    );

    writedump(arrTitles);
</cfscript>

Please refer this runnable example.

4
  • header in the request Can you be more specific? What is the context for this "request"? Commented Nov 24, 2015 at 21:38
  • This is for a REST API. So the client is using the Accept header in their request to send in stuff like application/vnd.mywebsite+json; version=1 or application/vnd.mywebsite+xml; version=2 as part of that. I am parsing that out and picking out the media type and version to serve, but I was wondering if I could do that faster with a regex. Commented Nov 25, 2015 at 11:49
  • I am parsing that out and picking out the media type and version to serve Okay. For next time, please include your current code as well. Questions that do not indicate what was already attempted (and results, error message, etcetera), closed as off-topic and down voted. Commented Nov 27, 2015 at 17:39
  • Typo: That should have read "...*tend* to be closed as off-topic..." Commented Nov 27, 2015 at 19:43

1 Answer 1

2

You could use something simple like this:

application/vnd.mywebsite\+([A-Za-z]+);\s*version=(\d+)

The type (json or xml) would be in capturing group 1, the version in group 2.

You can see it working here.

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

7 Comments

What if there is stuff before and after? Like: something=eeffe; application/vnd.mywebsite+json; version=1; else=rerhr
@EvagorasCharalambous - Shouldn't matter, as written the regex doesn't have any anchors for begin / end string. Paste your example into the link in my answer, you'll see it only matches the middle part.
So I am trying to get those out using ColdFusion, and the pattern just matches the whole string. Any ideas? trycf.com/gist/916e30f382fba677825c/acf11?theme=monokai . Ideally, I want an array of 2 elements, one with the value 'json' and another with '1'.
I don't think CF can do multiple group matching, so I will need to do each one separately. I am marking this as correct since it seems to have the correct regex - I just need to separate them.
RE: What if there is stuff before and after? Just noticed that. Here is a new version that should work: trycf.com/gist/585fc4863179825934d6/acf11?theme=monokai
|

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.