(^.)(\w+)(.$) $2
removes first and last char, but I'm not sure how does it work.
(^。)(\ w +)(。$)$ 2删除第一个和最后一个字符,但我不确定它是如何工作的。
My understanding:
(^.) match any one character at the start of the line. (.$) match any one character at the end of the line. (\w+) any word character(at least one character is required) $2 calls the second parentheses (\w+)
(^。)匹配行首的任何一个字符。 (。$)匹配行尾的任何一个字符。 (\ w +)任何单词字符(至少需要一个字符)$ 2调用第二个括号(\ w +)
Test1:
Input: 91239
Output: 123
Test2:
Input: \123\
Output: 123
Why does it remove the backslash? Is this a acceptable way to remove the backslash(begin and end of the line)?
为什么删除反斜杠?这是一种可以接受的方法来删除反斜杠(行的开头和结尾)吗?
Test3:
Input: /123/5
Output: /123/5
I'm lost here. Why it doesn't work for /123/5.
我迷失在这里。为什么它不适用于/ 123/5。
Thank you!
2 个解决方案
#1
Why does it remove the backslash? Is this a acceptable way to remove the backslash(begin and end of the line)?
为什么删除反斜杠?这是一种可以接受的方法来删除反斜杠(行的开头和结尾)吗?
It removes the backslashes because .
matches any character, including \
. Group 1 is the first backslash, group 2 is every character but the first and last, group 3 is the last backslash.
它会删除反斜杠,因为。匹配任何字符,包括\。第1组是第一个反斜杠,第2组是每个字符但第一个和最后一个,第3组是最后一个反斜杠。
I'm lost here. Why it doesn't work for /123/5.
我迷失在这里。为什么它不适用于/ 123/5。
\w
matches 0-9, a-z, A-Z, and _. \w+
consumes 123
. The following .
consumes /
. The following $
doesn't match the remaining 5
, thus there is no match with that input.
\ w匹配0-9,a-z,A-Z和_。 \ w +消耗123.以下。消费/。以下$与剩余的5不匹配,因此与该输入不匹配。
#2
Why it doesn't work for /123/5.
为什么它不适用于/ 123/5。
\w
is equivalent to [a-zA-Z0-9_]
and .
matches any character.. so in /123/5
.. /
before 1
is matched by ^.
and 5
is matched by .$
but 123/
is not matched since /
is not a matched by \w
\ w相当于[a-zA-Z0-9_]和。匹配任何字符..所以在/ 123/5 .. /之前1匹配^。和$匹配。$但是123 /不匹配,因为/不匹配\ w
Regex (^.)(\w+)(.$)
means (Explanation):
正则表达式(^。)(\ w +)(。$)表示(解释):
-
(^.)
start with any character (parenthesis => capture group 1) -
(\w+)
followed by more than one(+)
characters in the set[a-zA-Z0-9_]
(parenthesis => capture group 2) -
(.$)
end with any character (parenthesis => capture group 3)
(^。)以任何字符开头(括号=>捕获组1)
(\ w +)后跟集合中的多个(+)字符[a-zA-Z0-9_](括号=>捕获组2)
(。$)以任何字符结尾(括号=>捕获组3)
And finally $2
means backreference to capture group 2.. i.e group captured by pattern (\w+)
.
最后$ 2表示反向捕获组2 ..即由模式捕获的组(\ w +)。
#1
Why does it remove the backslash? Is this a acceptable way to remove the backslash(begin and end of the line)?
为什么删除反斜杠?这是一种可以接受的方法来删除反斜杠(行的开头和结尾)吗?
It removes the backslashes because .
matches any character, including \
. Group 1 is the first backslash, group 2 is every character but the first and last, group 3 is the last backslash.
它会删除反斜杠,因为。匹配任何字符,包括\。第1组是第一个反斜杠,第2组是每个字符但第一个和最后一个,第3组是最后一个反斜杠。
I'm lost here. Why it doesn't work for /123/5.
我迷失在这里。为什么它不适用于/ 123/5。
\w
matches 0-9, a-z, A-Z, and _. \w+
consumes 123
. The following .
consumes /
. The following $
doesn't match the remaining 5
, thus there is no match with that input.
\ w匹配0-9,a-z,A-Z和_。 \ w +消耗123.以下。消费/。以下$与剩余的5不匹配,因此与该输入不匹配。
#2
Why it doesn't work for /123/5.
为什么它不适用于/ 123/5。
\w
is equivalent to [a-zA-Z0-9_]
and .
matches any character.. so in /123/5
.. /
before 1
is matched by ^.
and 5
is matched by .$
but 123/
is not matched since /
is not a matched by \w
\ w相当于[a-zA-Z0-9_]和。匹配任何字符..所以在/ 123/5 .. /之前1匹配^。和$匹配。$但是123 /不匹配,因为/不匹配\ w
Regex (^.)(\w+)(.$)
means (Explanation):
正则表达式(^。)(\ w +)(。$)表示(解释):
-
(^.)
start with any character (parenthesis => capture group 1) -
(\w+)
followed by more than one(+)
characters in the set[a-zA-Z0-9_]
(parenthesis => capture group 2) -
(.$)
end with any character (parenthesis => capture group 3)
(^。)以任何字符开头(括号=>捕获组1)
(\ w +)后跟集合中的多个(+)字符[a-zA-Z0-9_](括号=>捕获组2)
(。$)以任何字符结尾(括号=>捕获组3)
And finally $2
means backreference to capture group 2.. i.e group captured by pattern (\w+)
.
最后$ 2表示反向捕获组2 ..即由模式捕获的组(\ w +)。