简体   繁体   中英

Get regexp group only if matching

I have two strings

"#string1" (1111) {string2}
"#string1" (1111)

I'd like to write regex that would match three groups. For first string:

1 => #string1
2 => 1111
3 => string2

and for second string

1 => #string1
2 => 1111
3 =>

The problem is I can't figure out how to produce all three groups but make the third one empty if the string doesn't contain curly braces. I can either match all three groups for first string and none for the second or match first two groups for both strings.

How should me regexp look like?

尝试:

^"([^"]*)" \(([^)]*)\)(?: \{([^}]*)\})?$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM