regexvbscriptregex-groupoptional-values

Regex: Match optional string to group


I have a file describing objects in which some properties of the object are optional. For example (color is optional):

type=dog
sex=male
name=wolf
color=brown
type=dog
sex=male
name=bob
type=cat
sex=male
name=tom
color=black
type=dog
sex=female
name=simona
color=white

I'm looking for a regex that gives me a pair of properties for a dog "name" - "color". I'm waiting for something like this:

wolf - brown
bob - 
simona - white

I started with

type=dog[\s\S]*?name=(\w+)[\s\S]*?color=(\w+)

Which gives wrong:

wolf - brown
bob - black
simona - white

Then I made group from color(which gives the same) and added "?" quantifier:

type=dog[\s\S]*?name=(\w+)[\s\S]*?(color=(\w+))?

But, instead of the desired result I lost 2nd group in all matches:

wolf - 
bob - 
simona - 

What's wrong with my expression and how to achieve my goal. Please do not use Lookbehind, Lookahead and Conditionals. VBScript not implement them.

My example on regex101.com


Solution

  • Set regex.Multiline = True and use the following regex:

    ^type=dog[\s\S]*?^name=(\w+)(?:(?:(?!^type=)[\s\S])*?^color=(\w+))?
    

    See the regex demo

    Details