正则表达式匹配可以基于两行文本吗? [英] Can regex match be based on two lines of text?

查看:50
本文介绍了正则表达式匹配可以基于两行文本吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有

定义
abc
xyz
abc

def
abc
xyz
abc

我想匹配

xyz
abc

xyz
abc

整体

这是否可以使用最通用的 RegEx?那不是具有多行标志的 perl RegEx 或 .Net Regex.

Is this possible using the most generic RegEx possible? That is not the perl RegEx or .Net Regex which have multi line flags.

我想应该是 BNF 来匹配这个.

I guess it would be BNF to match this.

推荐答案

正则表达式适用于您提供给它们的任何文本,多行或其他形式.如果它碰巧包含换行符,那么它名义上是多行"文本,但您无需执行任何特殊操作即可将其与正则表达式匹配.换行符只是另一个字符.

Regexes work on whatever text you give them, multiline or otherwise. If it happens to contain linefeeds, then it's nominally "multiline" text, but you don't have to do anything special to match it with regexes. Linefeed is just another character.

多行标志"(或多行模式")这个名字让很多人感到困惑.该标志所做的只是改变 ^$ 锚的含义,允许它们在逻辑行的开头和结尾以及逻辑行的开头和结尾进行匹配全文.

The name "multiline flag" (or "multiline mode") confuses many people. All that flag does is change the meaning of the ^ and $ anchors, allowing them to match at the beginning and end of logical lines as well as the beginning and end of the whole text.

这篇关于正则表达式匹配可以基于两行文本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆