1

I want to match a curl command's header pattern like:

-H 'key: value'

or

-H "key: value"

This switch can appear in the middle somewhere or at the end of string.

My pattern:

>>> header_pattern = re.compile(' \-H (?:\'|\").+?:.+?(?:\'|\")(?:\s+|$)')

My string:

>>> a = " -H 'Authorization: Bearer xxx' -H 'Content-Type: text/plain' "

Now I attempted to find all instances of this pattern but its matching just the first pattern.

>>> headers = header_pattern.findall(a)
>>> headers
[" -H 'Authorization: Bearer xxx' "]
2
  • 2
    Yes because your pattern matches a space at the end and at the beginning. Change (?:\s+|$) to (?!\S) (not followed by a character that is not a whitespace) Commented Jun 28, 2016 at 17:53
  • (?:-H|\G)\s([\'"])[^\'"]+\1 Commented Jun 28, 2016 at 17:55

2 Answers 2

4

Why don't use argparse module instead of regular expressions:

import argparse
import shlex


parser = argparse.ArgumentParser()
parser.add_argument('command')
parser.add_argument('url')
parser.add_argument('-d', '--data')
parser.add_argument('-b', '--data-binary', default=None)
parser.add_argument('-H', '--header', action='append', default=[])
parser.add_argument('--compressed', action='store_true')


curl_command = "curl https://google.com  -H 'Authorization: Bearer xxx' -H 'Content-Type: text/plain'"

tokens = shlex.split(curl_command)
parsed_args = parser.parse_args(tokens)
print(parsed_args.header)

Prints ['Authorization: Bearer xxx', 'Content-Type: text/plain'].

(Inspired by uncurl package).

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

1 Comment

Thanks a lot. Though this is not answer to question's concern but really gives me a lot better approach to achieve what I am trying to do.!
2

You can use this regex to match all header options:

header_pattern = re.compile(r'-H\s*([\'"])(.+?)\1')

RegEx Demo

There is no real need to assert for whitespace before -H but you need you can use:

header_pattern = re.compile(r'(?<=\s)-H\s*([\'"])(.+?)\1')

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.