Ruby gsub / 正则表达式修饰符?

12

我在哪里可以找到有关gsub修饰符的文档? \a \b \c \1 \2 \3 %a %b %c $1 $2 %3等等?

具体来说,我看着这段代码... something.gsub(/%u/, unit) 这个%u是什么意思?

5个回答

11

首先,在 Ruby 正则表达式中,%u 并没有什么特殊含义:

mixonic@pandora ~ $ irb
irb(main):001:0> '%u'.gsub(/%u/,'heyhey')
=> "heyhey"

Ruby 1.8正则表达式的权威文档在Ruby Doc Bundle中:

Strings delimited by slashes are regular expressions. The characters right after latter slash denotes the option to the regular expression. Option i means that regular expression is case insensitive. Option i means that regular expression does expression substitution only once at the first time it evaluated. Option x means extended regular expression, which means whitespaces and commens are allowd in the expression. Option p denotes POSIX mode, in which newlines are treated as normal character (matches with dots).

The %r/STRING/ is the another form of the regular expression.

^
    beginning of a line or string 
$
    end of a line or string 
.
    any character except newline 
\w
    word character[0-9A-Za-z_] 
\W
    non-word character 
\s
    whitespace character[ \t\n\r\f] 
\S
    non-whitespace character 
\d
    digit, same as[0-9] 
\D
    non-digit 
\A
    beginning of a string 
\Z
    end of a string, or before newline at the end 
\z
    end of a string 
\b
    word boundary(outside[]only) 
\B
    non-word boundary 
\b
    backspace(0x08)(inside[]only) 
[ ]
    any single character of set 
*
    0 or more previous regular expression 
*?
    0 or more previous regular expression(non greedy) 
+
    1 or more previous regular expression 
+?
    1 or more previous regular expression(non greedy) 
{m,n}
    at least m but most n previous regular expression 
{m,n}?
    at least m but most n previous regular expression(non greedy) 
?
    0 or 1 previous regular expression 
|
    alternation 
( )
    grouping regular expressions 
(?# )
    comment 
(?: )
    grouping without backreferences 
(?= )
    zero-width positive look-ahead assertion 
(?! )
    zero-width negative look-ahead assertion 
(?ix-ix)
    turns on (or off) `i' and `x' options within regular expression.

These modifiers are localized inside an enclosing group (if any). (?ix-ix: ) turns on (or off) i' andx' options within this non-capturing group.

Backslash notation and expression substitution available in regular expressions.

祝你好运!


我在看这段代码... .gsub(/%u/, unit),%u是什么?我在你的文档中找不到%u,而且对于%的解释对我来说也不清楚。谢谢! - Blaine
3
“%u”并没有什么特别的,可以在IRB中尝试以下代码:'%u'.gsub(/%u/,'heyhey') #=> "heyhey"。在这个例子中,gsub会查找字符串“%u”,然后用第二个参数“heyhey”进行替换。 - mixonic

9
Zenspider的Quickref包含一个部分,解释了正则表达式中可以使用哪些转义序列(which escape sequences can be used in regexen),以及一个列出由regexp匹配设置的伪变量(pseudo variables)的部分。在gsub的第二个参数中,您只需写下带有反斜线而不是$的变量名称,它将被替换为应用正则表达式后该变量的值。如果您使用双引号字符串,则需要使用两个反斜杠。当使用gsub的块形式时,您可以直接使用变量。如果您返回一个包含\1等内容的字符串,那么它将不会被替换为$1。这仅发生在使用两个参数形式时。

我在看这段代码... .gsub(/%u/, unit),%u是什么?我在你的文档中找不到%u,而且对于%的解释对我来说也不清楚。谢谢! - Blaine
1
%u 是一个百分号后跟字母 u。在正则表达式中它没有特殊的含义,因此我认为文本中确实包含该序列。 - sepp2k

5
如果您在sub/gsub中使用块,您可以像这样访问组:
>> rx = /(ab(cd)ef)/
>> s = "-abcdef-abcdef"
>> s.gsub(rx) { $2 }
=> "cdgh-cdghi"

1
在 Ruby 1.9 的 Oniguruma 中,有一个关于正则表达式的良好文档这里

0

gsub 是 LUA 语言中的字符串替换函数。

LUA 正则表达式 中,%u 表示大写字母类。即它将匹配所有大写字母。同样,%l 将匹配小写字母。

LUA 正则表达式类模式


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接