ini 属性的匹配值
在 上一个问题中,我问如何匹配 ini 的部分属性价值。 我得到了答复,并且它在积极的前瞻性下效果很好。
现在,我正在尝试获取属性的值:(edit)
foo.bar.far.boo =
value
foo.bar.far.boo =
值
“某事”
foo.bar .far.boo = "某物"
值
"某物"
foo.bar.far.boo =
值
;一些评论
开始编辑
我想匹配“值”(粗体)。
任何未加引号的单词后面或后面或前面或前面或没有任何其他字符串(引号),并且仅在=(值部分)之后
结束编辑
所以我尝试使用“正向后查找”(?<==\s)\w+
,但它在 javascript 中不存在。
关于如何实现这一目标有什么想法吗?
顺便说一句,我无法使用该值来反转字符串,因为我无法访问它,我需要返回一个正则表达式来匹配,这是我唯一能做的事情。
开始编辑
我无法使用该值,因为我无权访问它。我正在为脚本创建一个插件 a ,我只能做类似的事情:
return /myregex/;
而无法访问实际值。
结束编辑
In a previous question, I asked how to match a part of the property of an ini value.
I got answered and it works great with a positive lookahead.
Now, I'm trying to get the value of a property : (edit)
foo.bar.far.boo =
value
foo.bar.far.boo =
value
"something"
foo.bar.far.boo = "something"
value
"something"
foo.bar.far.boo =
value
; some comments
start edit
I want to match "value" (in bold).
Any word which is not quoted followed or not, preceded or not by any other string (quoted) and only after the = (the value part)
end edit
So I tried to use a "positive lookbehind" (?<==\s)\w+
, but it doesn't exist in javascript.
Any ideas on how to achieve this?
By the way, I can't work the value to reverse the string, as I can't access it, I need to return a regex to match, that's the only thing I can do.
start edit
I can't work with the value, because I've no access to it. I'm creating a plugin a for a script, and I can only do something like that :
return /myregex/;
without having access to the actual value.
end edit
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑 - 如果 PCRE 与 Perl 5.10 兼容,则可以使用:
/=\s*(?:".*?"\s*)*\K\w+/
你试过
/(?==\s*(?:".* ?"\s*)*(\w+))/
?捕获机制应该只捕获前瞻中的 value () ,但我不确定 JS 。
Perl:
'value1'
'值2'
'value3'
'值4'
Edit - If the PCRE is compatable with Perl 5.10, this would work:
/=\s*(?:".*?"\s*)*\K\w+/
Have you tried
/(?==\s*(?:".*?"\s*)*(\w+))/
?The capture mechanism should grab just the value () in the look ahead, but I'm not sure on JS.
Perl:
'value1'
'value2'
'value3'
'value4'
编辑 - 修改,
我想我已经通过前瞻找到了满意的答案。条件是如果 ini 行是正确的形式,它将成功并在匹配变量中返回单个值。想要让它失败就很难了。但是,返回
''
可能表明该值未传递表单,它传递了表单但是合法的''
。无论哪种方式,正则表达式都会考虑失败条件并返回''
因为这是有意义的。以下是同一事物的两种变体:
# 变体 1:第一个 'value [value [value [value ...]]]'
<代码>/^(?!.*=)|(?:(?==\s*(?:;|(?:\W*"[^"]*"\W*)*$))| \b(?:\w+\s*)+\b(?:(?=\s*;.*$)|(?=\s*(?:[^"=]*"[^"]* "[^"]*)*$)))/
# 变体 2:第一个“值”
<代码>/^(?!.*=)|(?:(?==\s*(?:;|(?:\W*"[^"]*"\W*)*$))| \b\w+\b(?:(?=\s*;.*$)|(?=\s*(?:[^"=]*"[^"]*"[^"]*)* $)))/
Perl 代码测试用例:
输出:
'value0'
'值10'
'值2'
'value30 value31'
'value4'
''
'value61'
''
''
<代码>''
Edit - modified,
I think I have found a satisfactory answer using just look aheads. The conditions are if the ini line is the proper form, it will suceed and return a single value in the match variable. It will be hard to make it fail. However, a return of
''
could indicate either the value didn't pass form, it passed form but is legitemately''
. Either way, the regex accounts for failure conditions and returns a''
as this makes sense.Here are two variations of the same thing:
# Variation 1: first 'value [value [value [value ...]]]'
/^(?!.*=)|(?:(?==\s*(?:;|(?:\W*"[^"]*"\W*)*$))|\b(?:\w+\s*)+\b(?:(?=\s*;.*$)|(?=\s*(?:[^"=]*"[^"]*"[^"]*)*$)))/
# Variation 2: first 'value'
/^(?!.*=)|(?:(?==\s*(?:;|(?:\W*"[^"]*"\W*)*$))|\b\w+\b(?:(?=\s*;.*$)|(?=\s*(?:[^"=]*"[^"]*"[^"]*)*$)))/
A Perl code test case:
Output:
'value0'
'value10'
'value2'
'value30 value31'
'value4'
''
'value61'
''
''
''