本文介绍了正则表达式匹配以感叹号开头的整行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试匹配从 开始的整行文本!使用正则表达式.

I'm trying to match whole line of text starting from !with regex.

我做了这样的事情:/(!\w+\s+\S+)/ig 非常接近,但只有 2 个词.我想将单词匹配到新行.我也看到句子中间有剧透的问题.

I made something like this: /(!\w+\s+\S+)/ig which is pretty close, but only for 2 words. I would like to match words upto new line. Also I see problem with spoiler in the middle of sentence.

实例:http://www.rubular.com/r/MXmholsDwE>

推荐答案

你只需要

^!.*

查看更新的正则表达式演示

^ 匹配一行的开头(在 Ruby 中),! 将匹配文字 !.* 将匹配零个或多个字符而不是换行符(如果您使用的是 Ruby,我假设您使用的是 rubular 网站).

The ^ matches the start of a line (in Ruby), ! will match a literal ! and .* will match zero or more characters other than a newline (if you are using Ruby, which I assume from your use of the rubular Web site).

如果您使用 Ruby 以外的正则表达式,例如 JS、PHP 或 .NET,则需要指定 /m - MULTILINE - 修饰符(例如 /^!.*/gm 在 JavaScript 中).

If you are using a regex flavor other than Ruby, like JS, or PHP, or .NET, you need to specify the /m - MULTILINE - modifier (e.g. /^!.*/gm in JavaScript).

这篇关于正则表达式匹配以感叹号开头的整行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 18:22
查看更多