简体   繁体   English

Ruby Regexp字符类带有换行符,为什么不匹配?

[英]Ruby Regexp character class with new line, why not match?

I want to use this regex to match any block comment (c-style) in a string. 我想使用此正则表达式来匹配字符串中的任何块注释(c样式)。 But why the below does not? 但是为什么下面没有呢?

rblockcmt = Regexp.new "/\\*[.\s]*?\\*/"  # match block comment
p rblockcmt=~"/* 22/Nov - add fee update */"

==> nil 

And in addition to what Sir Swoveland posted, a . 除了Swoveland爵士张贴的内容外,还有一个. matches any character except a newline : 匹配除换行符以外的任何字符:

The following metacharacters also behave like character classes: 以下元字符的行为也类似于字符类:

/./ - Any character except a newline. /./除换行符外的任何字符。

https://ruby-doc.org/core-2.3.0/Regexp.html https://ruby-doc.org/core-2.3.0/Regexp.html

If you need . 如果需要的话. to match a newline, you can specify the m flag, eg /.*?/m 要匹配换行符,可以指定m标志,例如/.*?/m

Options 选项

The end delimiter for a regexp can be followed by one or more single-letter options which control how the pattern can match. 正则表达式的结束定界符后可以跟一个或多个单字母选项,这些选项控制模式的匹配方式。

/pat/i - Ignore case /pat/i忽略大小写
/pat/m - Treat a newline as a character matched by . /pat/m将换行符视为与匹配的字符.
... ...

https://ruby-doc.org/core-2.3.0/Regexp.html https://ruby-doc.org/core-2.3.0/Regexp.html

Because having exceptions/quirks like newline not matching a . 因为有例外/怪癖(例如换行符)不匹配. can be painful, some people specify the m option for every regex they write. 可能会很痛苦,有些人为他们编写的每个正则表达式指定了m选项。

It appears that you intend [.\\s]*? 您似乎打算[.\\s]*? to match any character or a whitespace, zero or more times, lazily. 懒惰地匹配零个或多个字符或空格。 Firstly, whitespaces are characters, so you don't need \\s . 首先,空格是字符,因此您不需要\\s That simplifies your expression to [.]*? 这简化了您对[.]*?表达[.]*? . Secondly, if your intent is to match any character there is no need for a character class, just write . 其次,如果您要匹配任何字符,则不需要字符类,只需编写即可. . Thirdly, and most importantly , a period within a character class is simply the character "." 第三, 也是最重要的是 ,字符类中的句点就是字符"." .

You want .*? 你想要.*? (or [^*]* ). (或[^*]* )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM