Click here to Skip to main content
15,908,111 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Updated 31-Jul-18 23:30pm
v2

Try:
'\\$'

Then go here: Pythex: a Python regular expression editor[^] - it lets you test and develop regexes for python.
 
Share this answer
 
Quote:
But it's not working.

Is not help us to understand what you want.
Quote:
I want to print the line number of the line which ends with backslash in python

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

Quote:
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
PHP
test= "Hi\nHello \\\nGood Morning \\\nAll\nProgrammers"

then a RegEx that will match
"Hello \\\nGood Morning \\\nAll"

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[^]
 
Share this answer
 
Comments
Pruthvi@123 1-Aug-18 5:43am    
Thank You.But it's not an array.it is the lines of the text file.
Patrice T 1-Aug-18 5:46am    
your code is not autonomous and we can only guess.
In any case "for Line in fp:" is not magically split the text file in lines.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900