Ignore regular expressions?

Asked 2 years ago, Updated 2 years ago, 46 views

'--option1"value1" --option2""spacing"

{
    "option1": "value1",
    "2 option" : "space writing"
}

I need a code that makes it this way.

The problem here is that if I split based on spacing, there may be an error because there is a space in "", but is there a regular expression or code that can be split based on spacing, ignoring the space in ""?

python regex

2022-09-20 22:02

1 Answers

If you experiment with regular expressions on the regex101.com site, most of them will be solved.

Tested on regex101 site

If we implement it with Python code, it would be as follows.

import re, pprint
text = '--option1 "value1" --option2 "spacing"
match_objects = re.compile(r'--(option\d)\s*\"([^\"]+)\"\s*').findall(text)
match_dicts = {k:v for k, v in match_objects}
pprint.PrettyPrinter(indent=4, width=30).pprint(match_dicts)

But... "Spaced out"The front Two double quotes are correct, right? First of all, I thought "spacing" was the intended example.


2022-09-20 22:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.