匹配换行符-\ n或\ r \ n? [英] Match linebreaks - \n or \r\n?

查看:154
本文介绍了匹配换行符-\ n或\ r \ n?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编写此答案时,我必须完全匹配换行符,而不是使用s -flag(dotall-点与换行符匹配).

While writing this answer, I had to match exclusively on linebreaks instead of using the s-flag (dotall - dot matches linebreaks).

当尝试在\n\r\n上进行匹配时,通常用于测试正则表达式的网站的行为会有所不同.

The sites usually used to test regular expressions behave differently when trying to match on \n or \r\n.

我注意到了

  • Regex101仅在\n
    上匹配换行符 (示例-删除\r并匹配)

  • Regex101 matches linebreaks only on \n
    (example - delete \r and it matches)

RegExr匹配>上 的换行符不匹配\r\n
上的
的换行符 而且除了m -flag和\s
之外,我找不到能使其与换行符匹配的东西 (示例)

RegExr matches linebreaks neither on \n nor on \r\n
and I can't find something to make it match a linebreak, except for the m-flag and \s
(example)

Debuggex的行为更加不同:
此示例中,它仅在\r\n上匹配,而
此处仅在\n上匹配,并指定了相同的标志和引擎

Debuggex behaves even more different:
in this example it matches only on \r\n, while
here it only matches on \n, with the same flags and engine specified

我完全了解m标志(多行-使^与行的开头和$匹配行的末尾),但是有时候这不是一个选择.与\s相同,因为它也匹配制表符和空格.

I'm fully aware of the m-flag (multiline - makes ^ match the start and $ the end of a line), but sometimes this is not an option. Same with \s, as it matches tabs and spaces, too.

我想使用Unicode换行符( \u0085 )不成功,所以:

My thought to use the unicode newline character (\u0085) wasn't successful, so:

  1. 是否存在一种故障保险方法,可以将换行符上的匹配项(最好与所用语言无关)集成到正则表达式中?
  2. 为什么上述站点的行为有所不同(尤其是Debuggex,仅在\n上匹配一次,而在\r\n上匹配一次)?
  1. Is there a failsafe way to integrate the match on a linebreak (preferably regardless of the language used) into a regular expression?
  2. Why do the above mentioned sites behave differently (especially Debuggex, matching once only on \n and once only on \r\n)?

推荐答案

将以相反的方向回答;)

Gonna answer in opposite direction ;)

2)有关\ r和\ n的完整说明,我必须参考此问题,该问题比我在此处发布的问题要完整得多:

2) For a full explanation about \r and \n I have to refer to this question, which is far more complete than I will post here: Difference between \n and \r?

长话短说,Linux使用\ n换行,Windows \ r \ n和旧的Macs \ r.因此,可以使用多种方式编写换行符.例如,您的第二个工具(RegExr)与单个\ r匹配.

Long story short, Linux uses \n for a new-line, Windows \r\n and old Macs \r. So there are multiple ways to write a newline. Your second tool (RegExr) does for example match on the single \r.

1)Ilya建议的[\r\n]+可以工作,但也可以匹配多个连续的换行符. (\r\n|\r|\n)更正确.

1) [\r\n]+ as Ilya suggested will work, but will also match multiple consecutive new-lines. (\r\n|\r|\n) is more correct.

这篇关于匹配换行符-\ n或\ r \ n?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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