I am using Ruby1.9.3. I am newbie to this platform.
我使用的是Ruby1.9.3。我是这个平台的新手。
From the doc I just got familiared with two anchor
which are \z
and \G
. Now I little bit played with \z
to see how it works, as the definition(End or End of String) made me confused, I can't understand what it meant say - by End
. So I tried the below small snippets. But still unable to catch.
从文档中我刚刚得到了两个锚,它们是\ z和\ G.现在我和\ z一起玩它看看它是如何工作的,因为定义(字符串的结束或结束)让我感到困惑,我无法理解它的含义 - 由End。所以我尝试了下面的小片段。但仍然无法抓住。
CODE
码
irb(main):011:0> str = "Hit him on the head me 2\n" + "Hit him on the head wit>
=> "Hit him on the head me 2\nHit him on the head with a 24\n"
irb(main):012:0> str =~ /\d\z/
=> nil
irb(main):013:0> str = "Hit him on the head me 24 2\n" + "Hit him on the head >
=> "Hit him on the head me 24 2\nHit him on the head with a 24\n"
irb(main):014:0> str =~ /\d\z/
=> nil
irb(main):018:0> str = "Hit1 him on the head me 24 2\n" + "Hit him on the head>
=> "Hit1 him on the head me 24 2\nHit him on the head with a11 11 24\n"
irb(main):019:0> str =~ /\d\z/
=> nil
irb(main):020:0>
Every time I got nil
as the output. So how the calculation is going on for \z
? what does End
mean? - I think my concept took anything wrong with the End
word in the doc. So anyone could help me out to understand the reason what is happening with the out why so happening?
每次我得到零作为输出。那么\ z的计算方法是怎样的? End意味着什么? - 我认为我的概念在文档中的结束词出错了。所以任何人都可以帮助我了解发生这种情况的原因是什么?
And also i didn't find any example for the anchor \G
. Any example please from you people to make visualize how \G
used in real time programming?
而且我没有找到任何关于锚\ G的例子。任何一个例子请你们让人们想象一下如何用于实时编程?
EDIT
编辑
irb(main):029:0>
irb(main):030:0* ("{123}{45}{6789}").scan(/\G(?!^)\{\d+\}/)
=> []
irb(main):031:0> ('{123}{45}{6789}').scan(/\G(?!^)\{\d+\}/)
=> []
irb(main):032:0>
Thanks
谢谢
2 个解决方案
#1
5
\z
matches the end of the input. You are trying to find a match where 4 occurs at the end of the input. Problem is, there is a newline at the end of the input, so you don't find a match. \Z
matches either the end of the input or a newline at the end of the input.
\ z匹配输入的结尾。您正在尝试找到输入结尾处出现4的匹配项。问题是,输入结尾有一个换行符,所以找不到匹配项。 \ Z匹配输入的结尾或输入结尾的换行符。
So:
所以:
/\d\z/
matches the "4" in:
匹配“4”:
"24"
and:
和:
/\d\Z/
matches the "4" in the above example and the "4" in:
匹配上例中的“4”和“4”:
"24\n"
Check out this question for example of using \G
:
Examples of regex matcher \G (The end of the previous match) in Java would be nice
查看这个问题,例如使用\ G:Java中的正则表达式匹配器\ G(上一个匹配的结尾)的例子会很好
UPDATE: Real-World uses for \G
更新:真实世界用于\ G
I came up with a more real world example. Say you have a list of words that are separated by arbitrary characters that cannot be well predicted (or there's too many possibilities to list). You'd like to match these words where each word is its own match up until a particular word, after which you don't want to match any more words. For example:
我想出了一个更真实的例子。假设您有一个由任意字符分隔的单词列表,这些单词无法很好地预测(或列出的可能性太多)。你想匹配这些单词,其中每个单词都是自己的匹配,直到某个特定单词,之后你不想再匹配任何单词。例如:
foo,bar.baz:buz'fuzz*hoo-har/haz|fil^bil!bak
FOO,bar.baz:buz'fuzz *灏HAR / HAZ | FIL ^ BIL李明博
You want to match each word until 'har'. You don't want to match 'har' or any of the words that follow. You can do this relatively easily using the following pattern:
你想匹配每个单词,直到'har'。您不希望匹配“har”或后面的任何单词。您可以使用以下模式相对轻松地完成此操作:
/(?<=^|\G\W)\w+\b(?<!har)/
rubular
The first attempt will match the beginning of the input followed by zero non-word character followed by 3 word characters ('foo') followed by a word boundary. Finally, a negative lookbehind assures that the word which has just been matched is not 'har'.
第一次尝试将匹配输入的开头,后跟零非单词字符,后跟3个单词字符('foo'),后跟单词边界。最后,负面的背后保证确保刚刚匹配的单词不是“har”。
On the second attempt, matching picks back up at the end of the last match. 1 non-word character is matched (',' - though it is not captured due to the lookbehind, which is a zero-width assertion), followed by 3 characters ('bar').
在第二次尝试时,在最后一场比赛结束时匹配选秀权。 1个非单词字符匹配(',' - 虽然由于lookbehind而没有捕获,这是一个零宽度断言),后跟3个字符('bar')。
This continues until 'har' is matched, at which point the negative lookbehind is triggered and the match fails. Because all matches are supposed to be "attached" to the last successful match, no additional words will be matched.
这一直持续到'har'匹配,此时触发负面的后视并且匹配失败。因为所有匹配都应该“附加”到上一次成功匹配,所以不会匹配其他单词。
The result is:
结果是:
foo
bar
baz
buz
fuzz
hoo
If you want to reverse it and have all words after 'har' (but, again, not including 'har'), you can use an expression like this:
如果你想要反转它并在'har'之后拥有所有单词(但是,再次,不包括'har'),你可以使用这样的表达式:
/(?!^)(?<=har\W|\G\W)\w+\b/
rubular
This will match either a word which is immediately preceeded by 'har' or the end of the last match (except we have to make sure not to match the beginning of the input). The list of matches is:
这将匹配一个紧接在'har'之前的单词或最后一个匹配结束的单词(除了我们必须确保不匹配输入的开头)。比赛列表是:
haz
fil
bil
bak
If you do want to match 'har' and all following words, you could use this:
如果你想匹配'har'和所有后面的单词,你可以使用这个:
/\bhar\b|(?!^)(?<=\G\W)\w+\b/
rubular
This produces the following matches:
这会产生以下匹配:
har
haz
fil
bil
bak
#2
2
Sounds like you want to know how Regex works? Or do you want to know how Regex works with ruby?
听起来你想知道Regex是如何工作的?或者你想知道Regex如何使用ruby?
Check these out.
看看这些。
Regexp类描述
The Regex Coach - Great for testing regex matching
正则表达式教练 - 非常适合测试正则表达式匹配
正则表达式备忘单
I understand \G to be a boundary match character. So it would tell the next match to start at the end of the last match. Perhaps since you haven't made a match yet you cant have a second.
我理解\ G是一个边界匹配字符。所以它会告诉下一场比赛在最后一场比赛结束时开始。也许既然你还没有比赛,你就得不到第二个了。
Here is the best example I can find. Its not in ruby but the concept should be the same.
这是我能找到的最好的例子。它不是红宝石,但概念应该是相同的。
I take it back this might be more useful
我把它拿回去可能更有用
#1
5
\z
matches the end of the input. You are trying to find a match where 4 occurs at the end of the input. Problem is, there is a newline at the end of the input, so you don't find a match. \Z
matches either the end of the input or a newline at the end of the input.
\ z匹配输入的结尾。您正在尝试找到输入结尾处出现4的匹配项。问题是,输入结尾有一个换行符,所以找不到匹配项。 \ Z匹配输入的结尾或输入结尾的换行符。
So:
所以:
/\d\z/
matches the "4" in:
匹配“4”:
"24"
and:
和:
/\d\Z/
matches the "4" in the above example and the "4" in:
匹配上例中的“4”和“4”:
"24\n"
Check out this question for example of using \G
:
Examples of regex matcher \G (The end of the previous match) in Java would be nice
查看这个问题,例如使用\ G:Java中的正则表达式匹配器\ G(上一个匹配的结尾)的例子会很好
UPDATE: Real-World uses for \G
更新:真实世界用于\ G
I came up with a more real world example. Say you have a list of words that are separated by arbitrary characters that cannot be well predicted (or there's too many possibilities to list). You'd like to match these words where each word is its own match up until a particular word, after which you don't want to match any more words. For example:
我想出了一个更真实的例子。假设您有一个由任意字符分隔的单词列表,这些单词无法很好地预测(或列出的可能性太多)。你想匹配这些单词,其中每个单词都是自己的匹配,直到某个特定单词,之后你不想再匹配任何单词。例如:
foo,bar.baz:buz'fuzz*hoo-har/haz|fil^bil!bak
FOO,bar.baz:buz'fuzz *灏HAR / HAZ | FIL ^ BIL李明博
You want to match each word until 'har'. You don't want to match 'har' or any of the words that follow. You can do this relatively easily using the following pattern:
你想匹配每个单词,直到'har'。您不希望匹配“har”或后面的任何单词。您可以使用以下模式相对轻松地完成此操作:
/(?<=^|\G\W)\w+\b(?<!har)/
rubular
The first attempt will match the beginning of the input followed by zero non-word character followed by 3 word characters ('foo') followed by a word boundary. Finally, a negative lookbehind assures that the word which has just been matched is not 'har'.
第一次尝试将匹配输入的开头,后跟零非单词字符,后跟3个单词字符('foo'),后跟单词边界。最后,负面的背后保证确保刚刚匹配的单词不是“har”。
On the second attempt, matching picks back up at the end of the last match. 1 non-word character is matched (',' - though it is not captured due to the lookbehind, which is a zero-width assertion), followed by 3 characters ('bar').
在第二次尝试时,在最后一场比赛结束时匹配选秀权。 1个非单词字符匹配(',' - 虽然由于lookbehind而没有捕获,这是一个零宽度断言),后跟3个字符('bar')。
This continues until 'har' is matched, at which point the negative lookbehind is triggered and the match fails. Because all matches are supposed to be "attached" to the last successful match, no additional words will be matched.
这一直持续到'har'匹配,此时触发负面的后视并且匹配失败。因为所有匹配都应该“附加”到上一次成功匹配,所以不会匹配其他单词。
The result is:
结果是:
foo
bar
baz
buz
fuzz
hoo
If you want to reverse it and have all words after 'har' (but, again, not including 'har'), you can use an expression like this:
如果你想要反转它并在'har'之后拥有所有单词(但是,再次,不包括'har'),你可以使用这样的表达式:
/(?!^)(?<=har\W|\G\W)\w+\b/
rubular
This will match either a word which is immediately preceeded by 'har' or the end of the last match (except we have to make sure not to match the beginning of the input). The list of matches is:
这将匹配一个紧接在'har'之前的单词或最后一个匹配结束的单词(除了我们必须确保不匹配输入的开头)。比赛列表是:
haz
fil
bil
bak
If you do want to match 'har' and all following words, you could use this:
如果你想匹配'har'和所有后面的单词,你可以使用这个:
/\bhar\b|(?!^)(?<=\G\W)\w+\b/
rubular
This produces the following matches:
这会产生以下匹配:
har
haz
fil
bil
bak
#2
2
Sounds like you want to know how Regex works? Or do you want to know how Regex works with ruby?
听起来你想知道Regex是如何工作的?或者你想知道Regex如何使用ruby?
Check these out.
看看这些。
Regexp类描述
The Regex Coach - Great for testing regex matching
正则表达式教练 - 非常适合测试正则表达式匹配
正则表达式备忘单
I understand \G to be a boundary match character. So it would tell the next match to start at the end of the last match. Perhaps since you haven't made a match yet you cant have a second.
我理解\ G是一个边界匹配字符。所以它会告诉下一场比赛在最后一场比赛结束时开始。也许既然你还没有比赛,你就得不到第二个了。
Here is the best example I can find. Its not in ruby but the concept should be the same.
这是我能找到的最好的例子。它不是红宝石,但概念应该是相同的。
I take it back this might be more useful
我把它拿回去可能更有用