在Python中查找以'\'(反斜杠)结尾的行的正则表达式是什么? [英] What is the regular expression for finding line ending with '\'(backslash) in Python?

查看:265
本文介绍了在Python中查找以'\'(反斜杠)结尾的行的正则表达式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在python中打印以反斜杠结尾的行的行号,并希望将该行合并到Linenumber,其中包含'\'+ 1



eg





你好\

早安\

所有

程序员



最后的文字应该是

Hello Good Morning All



我不知道如何根据这个条件进行合并。



我尝试了什么:



I want to print the line number of the line which ends with backslash in python and want to merge the line till Linenumber which contains '\' + 1

e.g

Hi
Hello \
Good Morning \
All
Programmers

The final text should be
Hello Good Morning All

I don't know how to merge based on this condition.

What I have tried:

for Line in fp:
    if (re.search('(\\)', Line)):
         print(LineNumber)
         LineNumber += 1
         continue



但它不起作用。


But it's not working.

推荐答案

尝试:

Try:
'\\


'



然后转到此处: Pythex:Python正则表达式编辑器 [ ^ ] - 它允许你测试和开发python的正则表达式。


Then go here: Pythex: a Python regular expression editor[^] - it lets you test and develop regexes for python.


Quote:

但它不起作用。



帮助我们理解你想要的东西。


Is not help us to understand what you want.

引用:

我想在python中打印以反斜杠结尾的行的行号

I want to print the line number of the line which ends with backslash in python



尝试


Try

for Line in fp:
    if (re.search('(\\)', Line)):
         print(LineNumber)
    LineNumber += 1
    continue




引用:

想要将该行合并到Linenumber,其中包含'\'+ 1

want to merge the line till Linenumber which contains '\' + 1



这是另一个问题。

你需要用字符串代替数组:


This is another problem.
you need to have this in a string instead of an array:

Hi
Hello \
Good Morning \
All
Programmers



类似


Something like

test= "Hi\nHello \\\nGood Morning \\\nAll\nProgrammers"



然后是一个匹配的RegEx


then a RegEx that will match

"Hello \\\nGood Morning \\\nAll"



一旦你在一个字符串中得到匹配,替换\\\ n



只是一些有趣的链接到帮助构建和调试RegEx。

以下是RegEx文档的链接:

perlre - perldoc.perl.org [ ^ ]

以下是帮助构建RegEx并调试它们的工具的链接:

.NET Regex Tester - Regex风暴 [ ^ ]

Expresso正则表达式工具 [ ^ ]

RegExr:Learn,Build,&测试RegEx [ ^ ]

在线正则表达式测试器和调试器:PHP,PCRE,Python,Golang和JavaScript [ ^ ]

这个显示RegEx是一个很好的图表,它非常有助于理解RegEx的作用:

Debuggex:在线可视正则表达式测试器。 JavaScript,Python和PCRE。 [ ^ ]

这个网站也在一个漂亮的图表中显示正则表达式,但无法测试与RegEx匹配的内容:

Regexper [ ^ ]


once you got the match in a single string, replace the "\\\n"

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx:
Regexper[^]


这篇关于在Python中查找以'\'(反斜杠)结尾的行的正则表达式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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