Matching beginning of line

A place to try and solve your RegEx problems.

Matching beginning of line

Postby ebulerdo » Thu Feb 16, 2017 9:46 am

Hello!

I am trying to write a regex that deletes all lines that do not begin with an HTML tag. In my text editor (Editpad) I would write it like this:
^[^<].+?\r\n

But in TextCrawler ^ only matches the beginning of the file, not every line.

I found a previous thread here (viewtopic.php?f=7&t=602) that suggests using:
\r\n[^<].*

But that option has two problems: 1) for some reason the * matches the final \r too, so it leaves the file with a mix of Windows and Unix-style ends of line, and 2) for that reason it only deletes half of the offending lines. If there are two consecutive lines that don't begin with < it will only remove the first one, the second one doesn't match anymore because it no longer is preceded by \r\n, only \n.

I improved that expression as:
\r\n[^<].*\r\n
and replacing it with \r\n

That way it no longer leaves Unix-style line returns, but still it only removes half of the lines, so I have to repeat the same search several times to get rid of all the lines. And still I have no way of makign sure that all lines have been removed, so I have to check manually.

This would all be easily resolved if there was an operator that matched the begining of line. Is there anything like that? Any suggestions?

Thanks!
ebulerdo
 
Posts: 4
Joined: Mon Dec 09, 2013 9:54 am

Re: Matching beginning of line

Postby ebulerdo » Thu Feb 16, 2017 10:13 am

This is an example to make it clearer what I want to do:

I have this text:

<keep>Keep this line</keep>
<keep>Keep this line</keep>
Remove this line
Remove this line too
<keep>Keep this line</keep>

And I want this:

<keep>Keep this line</keep>
<keep>Keep this line</keep>
<keep>Keep this line</keep>

Thank you!
ebulerdo
 
Posts: 4
Joined: Mon Dec 09, 2013 9:54 am

Re: Matching beginning of line

Postby ebulerdo » Thu Feb 16, 2017 3:38 pm

OK, I think I found the solution reading other topics.

I need to have Multiline anchors enabled.

Now it works. :-)

Thanks!
ebulerdo
 
Posts: 4
Joined: Mon Dec 09, 2013 9:54 am


Who is online

Users browsing this forum: No registered users and 3 guests