英文:
For this method, how is regex working to catch the first letter in return str.replaceAll("(\\w)(\\w*)", "$2$1a?
问题
对于下面的问题和方法,当括号中的两个组都捕获单词时,如何将每个单词的第一个字母移到末尾?我理解正则表达式中的星号表示我们匹配此模式0次或多次。我也理解在“$2$1ay”中这两个组内部有一个交换。我只是不明白第一个字母是如何被捕获的。
问题:
将每个单词的第一个字母移到末尾,然后在单词后添加“ay”。不要改动标点符号。
答案所使用的方法:
public class PigLatin {
public static String pigIt(String str) {
return str.replaceAll("(\\w)(\\w*)", "$2$1ay");
}
}
英文:
For the question and method below, how is this moving the first letter of each word, when both groups in the parenthesis are catching words? I understand that asterisk in regex means we are matching this pattern 0 or more times. I also understand that where is a swap in these two groups inside the parenthesis in “$2$1ay”. I just don’t see how the first letter is being captured.
Question:
Move the first letter of each word to the end of it, then add "ay" to the end of the word. Leave punctuation marks untouched.
Method used for answer:
public class PigLatin {
public static String pigIt(String str) {
return str.replaceAll("(\\w)(\\w*)", "$2$1ay");
}
}
答案1
得分: 3
简短回答
> 在返回str.replaceAll("(\\w)(\\w*)", "$2$1ay")
中,正则表达式如何工作以捕获第一个字母?我只是不明白第一个字母是如何被捕获的。
\w
匹配任何单个单词字符,而不是多于一个单词字符。它等同于 [a-zA-Z0-9_]
而不是 [a-zA-Z0-9_]+
。
解释
在你的正则表达式 (\w)(\w*)
(在 Java 中表示为 "(\\w)(\\w*)"
),(\w)
是第一个捕获组($1
),它捕获单个单词字符。(\w*)
是第二个捕获组($2
),它捕获零个或多个单词字符。替换字符串 $2$1ay
交换了两个捕获组,并在末尾附加了 ay
,就像你提到的一样。
在这个例子中,你可以通过在 regex101.com 上进行详细理解,使用彩色编码的可视化,并将其用于以后参考。
英文:
Short answer
> How is regex working to catch the first letter in return str.replaceAll(“(\w)(\w*)”, "$2$1a? I just don’t see how the first letter is being captured.
\w
matches any single word character and not more than one word characters. It is equal to [a-zA-Z0-9_]
and not [a-zA-Z0-9_]+
.
Explanation
In your regex (\w)(\w*)
("(\\w)(\\w*)"
as String in Java), (\w)
is 1st capturing group ($1
) which captures a single word character. (\w*)
is 2nd capturing group ($2
) which captures zero or more word characters. $2$1ay
as replace string swaps both groups and appends ay
just like you mentioned.
Check this example on regex101.com for detailed understanding with color-coded visualization and for future references on regex.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论