有什么区别”[^“*”和“+”

时间:2022-04-17 13:54:00

What is the difference between using the two following statements in a cucumber step definition? When I tested them in Rubular, they both worked in all the cases I could imagine. In the case of the second, my syntax highlighting is more likely to look good (no extra double quote to mess things up).

在黄瓜步骤定义中使用以下两个语句的区别是什么?当我用小舌做测试时,它们都适用于我能想到的所有情况。在第二种情况下,我的语法突出显示更可能看起来不错(没有额外的双引号把事情搞砸)。

Even in the Stack Overflow syntax highlighting, it gets goofed up on the first. What are the advantages of the more common first example?

即使在堆栈溢出语法突出显示中,第一次也会出错。比较常见的第一个例子的优点是什么?

Given /^My name is "([^"]*)"$/ do |myname|
Given /^My name is "(.+)"$/ do |myname|

4 个解决方案

#1


6  

[^"]* means N (N>=0) characters except "

[^ "]*意味着N(N > = 0)字符除了“

.+ means N (N>0) characters including "

.+表示N (N>)个字符,包括"

If the subject is more than 1 char, and without quote mark("), the two regex patterns are equal.

如果主题超过1个字符,并且没有引号(“),两个正则表达式是相等的。

But, consider this string: My name is "special_name_contain_"_laugh" Run your pattern again, they are NOT the same :)

但是,请考虑这个字符串:我的名字是“special_name_contain_”“_laugh”,再次运行您的模式,它们不是相同的:)

#2


5  

The first will not break when provided the following:

当提供以下内容时,第一个不会中断:

My name is "Henry James" and some other condition is "something else"

The first regular expression limits the characters inside the quoted string to non-quote characters - thus it will only pick up Henry James. The second regular expression matches a quote character followed by anything else (including other quote characters) and then an ending quote character - so myname in the second case would be:

第一个正则表达式将引用字符串中的字符限制为非引用字符——因此它只会选择Henry James。第二个正则表达式匹配一个引号字符,后面跟着任何其他字符(包括其他引号字符),然后是一个引号字符——因此第二个例子中的myname将是:

Henry James" and some other condition is "something else

Which means that you can have only one quoted value in your test case - which is far more limiting than the limitation of the first regular expression (you can only have quoted values that do not contain a quote character).

这意味着您可以在您的测试用例中只有一个引用值——这比第一个正则表达式的限制要限制得多(您只能引用不包含引号字符的值)。

#3


1  

I'm not a ruby guru, however the first regular expression means:

我不是ruby的权威,但是第一个正则表达式意味着:

  • ^ Start of line
  • ^开始行
  • My name is " the literal string followed by a "
  • 我的名字是"字符串后面跟着a "
  • ( starts a capture group
  • (启动捕获组
  • [ starts a character class
  • 开始一个角色类
  • ^" in a character class, ^ means "not", so in this case it means anything but a "
  • ”^ ^字符类,意思是“不”,所以在这种情况下这意味着除了“
  • ] end of character class
  • 结束字符类。
  • * any number of the preceding, including 0 matches
  • *前面的任何数字,包括0匹配。
  • " a quote character
  • “引用字符
  • $ end of line
  • 美元行结束

The second one, everything is the same as above, except in the character class [] you have:

第二个,所有的都和上面一样,除了在人物类[]中你有:

  • . "any character"
  • 。“任何字符”
  • + one or more of the preceding
  • +前面的一个或多个

The difference between + and * is that + requires atleast one of the preceeding, but * will also match if there are zero of the preceding.

+和*之间的区别是+至少需要一个前面的标记,但如果前面的标记为0,*也将匹配。

#4


1  

[^"] means any char except "

(^”)以外的任何字符”

. means any char.

。是指任何字符。

* means match any times include 0.

*表示匹配任何时间包含0。

+ means match at least 1 time.

+表示匹配至少一次。

#1


6  

[^"]* means N (N>=0) characters except "

[^ "]*意味着N(N > = 0)字符除了“

.+ means N (N>0) characters including "

.+表示N (N>)个字符,包括"

If the subject is more than 1 char, and without quote mark("), the two regex patterns are equal.

如果主题超过1个字符,并且没有引号(“),两个正则表达式是相等的。

But, consider this string: My name is "special_name_contain_"_laugh" Run your pattern again, they are NOT the same :)

但是,请考虑这个字符串:我的名字是“special_name_contain_”“_laugh”,再次运行您的模式,它们不是相同的:)

#2


5  

The first will not break when provided the following:

当提供以下内容时,第一个不会中断:

My name is "Henry James" and some other condition is "something else"

The first regular expression limits the characters inside the quoted string to non-quote characters - thus it will only pick up Henry James. The second regular expression matches a quote character followed by anything else (including other quote characters) and then an ending quote character - so myname in the second case would be:

第一个正则表达式将引用字符串中的字符限制为非引用字符——因此它只会选择Henry James。第二个正则表达式匹配一个引号字符,后面跟着任何其他字符(包括其他引号字符),然后是一个引号字符——因此第二个例子中的myname将是:

Henry James" and some other condition is "something else

Which means that you can have only one quoted value in your test case - which is far more limiting than the limitation of the first regular expression (you can only have quoted values that do not contain a quote character).

这意味着您可以在您的测试用例中只有一个引用值——这比第一个正则表达式的限制要限制得多(您只能引用不包含引号字符的值)。

#3


1  

I'm not a ruby guru, however the first regular expression means:

我不是ruby的权威,但是第一个正则表达式意味着:

  • ^ Start of line
  • ^开始行
  • My name is " the literal string followed by a "
  • 我的名字是"字符串后面跟着a "
  • ( starts a capture group
  • (启动捕获组
  • [ starts a character class
  • 开始一个角色类
  • ^" in a character class, ^ means "not", so in this case it means anything but a "
  • ”^ ^字符类,意思是“不”,所以在这种情况下这意味着除了“
  • ] end of character class
  • 结束字符类。
  • * any number of the preceding, including 0 matches
  • *前面的任何数字,包括0匹配。
  • " a quote character
  • “引用字符
  • $ end of line
  • 美元行结束

The second one, everything is the same as above, except in the character class [] you have:

第二个,所有的都和上面一样,除了在人物类[]中你有:

  • . "any character"
  • 。“任何字符”
  • + one or more of the preceding
  • +前面的一个或多个

The difference between + and * is that + requires atleast one of the preceeding, but * will also match if there are zero of the preceding.

+和*之间的区别是+至少需要一个前面的标记,但如果前面的标记为0,*也将匹配。

#4


1  

[^"] means any char except "

(^”)以外的任何字符”

. means any char.

。是指任何字符。

* means match any times include 0.

*表示匹配任何时间包含0。

+ means match at least 1 time.

+表示匹配至少一次。