ini 属性的匹配值

发布于 2024-10-18 11:30:24 字数 1039 浏览 5 评论 0原文

上一个问题中,我问如何匹配 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染火枫林 2024-10-25 11:30:24

编辑 - 如果 PCRE 与 Perl 5.10 兼容,则可以使用:
/=\s*(?:".*?"\s*)*\K\w+/

你试过 /(?==\s*(?:".* ?"\s*)*(\w+))/ ?
捕获机制应该只捕获前瞻中的 value () ,但我不确定 JS 。

Perl:

use strict;
use warnings;

my @ary = (

'foo.bar.far.boo = value1',
'foo.bar.far.boo = value2 "something"',
'foo.bar.far.boo = "something" value3 "something"',
'foo.bar.far.boo = value4 ; some comments',
'foo.bar.far.boo = ; some comments value5'
);

for (@ary) {
#    if ( /(?==\s*(?:".*?"\s*)*(\w+))/ ) {
#        print "'$1'\n";
# or, >= Perl 5.10 ...
    if ( /=\s*(?:".*?"\s*)*\K\w+/ ) {
        print "'
amp;'\n";
    }
}

'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:

use strict;
use warnings;

my @ary = (

'foo.bar.far.boo = value1',
'foo.bar.far.boo = value2 "something"',
'foo.bar.far.boo = "something" value3 "something"',
'foo.bar.far.boo = value4 ; some comments',
'foo.bar.far.boo = ; some comments value5'
);

for (@ary) {
#    if ( /(?==\s*(?:".*?"\s*)*(\w+))/ ) {
#        print "'$1'\n";
# or, >= Perl 5.10 ...
    if ( /=\s*(?:".*?"\s*)*\K\w+/ ) {
        print "'
amp;'\n";
    }
}

'value1'
'value2'
'value3'
'value4'

尐偏执 2024-10-25 11:30:24

编辑 - 修改,

我想我已经通过前瞻找到了满意的答案。条件是如果 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 代码测试用例:

use strict;
use warnings;

my @ary = (
'foo.bar.far.boo =  value0  ',
'foo.bar.far.boo = value10  ;  value11 ; more comments',
'foo.bar.far.boo = value2 "something2"',
'foo.bar.far.boo = value30 value31 "something32" value33 "something34"',
'foo.bar.far.boo = value4 ; some comments4',
'foo.bar.far.boo = ; some comments50 value51',
'foo.bar.far.boo = "something60" value61 "something62" value63 "something64"',
'foo.bar.far.boo    some comments70 value71',
'foo.bar.far.boo = ',
'foo.bar.far.boo = "junk90"  ,,  "junk91"',
);

for (@ary)
{
 if ( /
       ^ (?! .*=)
     |
       (?:
           (?= =\s*(?:;|(?:\W*"[^"]*"\W*)*$ ))
         |
            \b (?:\w+\s*)+ \b
            (?:
                (?= \s* ; .* $)
              | (?= \s* (?: [^"=]* "[^"]*" [^"]* )* $)
            )
        ) /x )
 {
    print "'
amp;'\n";
 }
}

输出:

'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:

use strict;
use warnings;

my @ary = (
'foo.bar.far.boo =  value0  ',
'foo.bar.far.boo = value10  ;  value11 ; more comments',
'foo.bar.far.boo = value2 "something2"',
'foo.bar.far.boo = value30 value31 "something32" value33 "something34"',
'foo.bar.far.boo = value4 ; some comments4',
'foo.bar.far.boo = ; some comments50 value51',
'foo.bar.far.boo = "something60" value61 "something62" value63 "something64"',
'foo.bar.far.boo    some comments70 value71',
'foo.bar.far.boo = ',
'foo.bar.far.boo = "junk90"  ,,  "junk91"',
);

for (@ary)
{
 if ( /
       ^ (?! .*=)
     |
       (?:
           (?= =\s*(?:;|(?:\W*"[^"]*"\W*)*$ ))
         |
            \b (?:\w+\s*)+ \b
            (?:
                (?= \s* ; .* $)
              | (?= \s* (?: [^"=]* "[^"]*" [^"]* )* $)
            )
        ) /x )
 {
    print "'
amp;'\n";
 }
}

Output:

'value0'
'value10'
'value2'
'value30 value31'
'value4'
''
'value61'
''
''
''

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文