I have a string which looks like the following:
我有一个字符串,如下所示:
VAL "foo"
VAL2 "bar"
VAL3 "barbar"
OPTIONALVAL "optionalvalue"
SPECIALVAL "optionalval"
Where OPTIONALVAL
is optional (may or may not appear) and if it appears then it has a value pair in the second column ("optionalvalue"
in the example), and SPECIALVAL
is also optional, but it has no paired value in the second column.
其中OPTIONALVAL是可选的(可能出现也可能不出现),如果出现,则它在第二列中有一个值对(示例中为“optionalvalue”),SPECIALVAL也是可选的,但在第二列中没有配对值。
Here is my regex, but it doesn't match anything if OPTIONALVAL
is missing (should match all the others which are not missing)!
这是我的正则表达式,但是如果缺少OPTIONALVAL,它就不匹配任何东西(应该匹配所有其他没有丢失的东西)!
"^[[:space:]]*"
"(VAL)[[:space:]]*\"(.*)\"[[:space:]]*"
"(VAL2)[[:space:]]*\"(.*)\"[[:space:]]*"
"(VAL3)[[:space:]]*\"(.*)\"[[:space:]]*"
"(OPTIONALVAL)?[[:space:]]*\"(?(7)(.*))\"[[:space:]]*"; // conditional capture
"(SPECIALVAL)?[[:space:]]*";
What am I doing wrong? (I am using Boost regex, :space:
should be equivalent to the space character.
我究竟做错了什么? (我正在使用Boost正则表达式,:space:应该等于空格字符。
1 个解决方案
#1
2
For your conditional capture, you can do something like (?:xyz)?
where xyz
is the conditional pattern.
对于条件捕获,您可以执行类似(?:xyz)的操作?其中xyz是条件模式。
VAL\s*"(.*?)"\s*
VAL2\s*"(.*?)"\s*
VAL3\s*"(.*?)"\s*
(?:OPTIONALVAL\s*"(.*?)"\s*)?
SPECIALVAL
Note that the live preview has the extended flag enabled, to ignore those newlines.
请注意,实时预览启用了扩展标志,以忽略这些换行符。
As your question reflects that you want to capture VAL
, etc. Then you of course need to surround them with parenthesis.
正如你的问题反映出你想要捕获VAL等等。那么你当然需要用括号括起它们。
The thing to notice, is that whether conditional capture is present or not. All the capture groups will still remain the same index-wise.
需要注意的是,是否存在条件捕获。所有捕获组仍将保持相同的索引。
#1
2
For your conditional capture, you can do something like (?:xyz)?
where xyz
is the conditional pattern.
对于条件捕获,您可以执行类似(?:xyz)的操作?其中xyz是条件模式。
VAL\s*"(.*?)"\s*
VAL2\s*"(.*?)"\s*
VAL3\s*"(.*?)"\s*
(?:OPTIONALVAL\s*"(.*?)"\s*)?
SPECIALVAL
Note that the live preview has the extended flag enabled, to ignore those newlines.
请注意,实时预览启用了扩展标志,以忽略这些换行符。
As your question reflects that you want to capture VAL
, etc. Then you of course need to surround them with parenthesis.
正如你的问题反映出你想要捕获VAL等等。那么你当然需要用括号括起它们。
The thing to notice, is that whether conditional capture is present or not. All the capture groups will still remain the same index-wise.
需要注意的是,是否存在条件捕获。所有捕获组仍将保持相同的索引。