How to handle spaces in values if you use spaces as a delimiter with regex?

Flare :

I'm attempting to run a regex to capture the key and value of the following string:

name="Evoke Sprite" parent="EvokeObjects" instance=ExtResource( 5 ) id=5

Here are some syntax notes for each are as follows:

  • Keys: a string of letters, no spaces
  • Values:

    • May have quotation marks eg. "EvokeObjects"
    • May have spaces within the quotation marks
    • May have special characters within the quotation marks eg "hello/world@!18"
    • May have a function like string ("ExtResource( 5 )").
    • The function string will have spaces within the brackets

I've gotten as far as having spaces within quotes with this:

(.*?)=(?:"(.*?)"|(.*?))(?: |$)

So this will work with name="Evoke Sprite" parent="EvokeObjects" id=5

regex101 to test: https://regex101.com/r/xkRRsD/1

The problem occurs when I add the ExtResource( 5 ) because it has the space within the brackets. Then the previous regex code fails.

As a possible workaround I was thinking maybe I could remove the spaces altogether from the brackets by doing a string replace in code. But I was wondering if there was a regex solution to this?

The fourth bird :

In the second part of the alternation, you match until a space or the end of the string so that would match ExtResource(

What you could do is either match not a parenthesis or match from an opening till a closing parenthesis.

Instead of using non greedy quantifiers, you might use a negated character class.

([^=\s]+)=(?:"([^"]+)"|((?:[^\s()"]|\([^()]*\))+))

Explanation

  • ([^=]+)= Capture group 1, match any char except =, then match =
  • (?: Non capturing groups
    • "([^"]*)" Match ", then capture any char except " in group 2, then match "
    • | Or
    • ( Capture group 3
      • (?: Non capturing group
        • [^\s()"] Match any char except (, ), " or a whitespace char
        • | Or
        • \([^()]*\) Match from opening till closing parenthesis
      • )+ Close non capturing group and repeat 1+ times
    • ) Close group 3
  • ) Close non capturing group

Regex demo

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=144182&siteId=1