regex not selecting last end of line

Questions about XML that are not covered by the other forums should go here.
amjc
Posts: 3
Joined: Fri Mar 31, 2023 3:18 pm

regex not selecting last end of line

Post by amjc »

Hi, I want to delete (i.e. replace wiith nothing) the first three lines of this code
<row>
<entry namest="col1" nameend="col2"/>
</row>
<row>
and when I use this regex
<row>$\s*<entry namest="col1" nameend="col2"\/>$\s*<\/row>$\s
It doesn't select the final eol, so it ends up replacing the 3 unwanted lines with a blank line.
Any suggestions? Would also be open to something using xpath.
adrian
Posts: 2858
Joined: Tue May 17, 2005 4:01 pm

Re: regex not selecting last end of line

Post by adrian »

Hi,

I don't see a problem with the regex, it should also match the line break after the closing row tag. In Oxygen the line breaks in XML are strictly \n (a single whitespace).
You may want to also add ^\s* in front, so that it consumes the indent in front of the row start tag. Perhaps that's what you see at the end.

XPath would be: //row[entry[@namest="col1" and @nameend="col2"]]
But note that in case of XPath moreso you can't remove the trailing line break as it is outside of the row tags.
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
EmmaAlva
Posts: 1
Joined: Thu Aug 01, 2024 1:02 pm

Re: regex not selecting last end of line

Post by EmmaAlva »

To delete the first three lines of your XML code using a regex, you can use a regex that matches the lines and includes the newline characters at the end tiny fishing
Here's a refined regex that should work for you:
<row>\s*<entry namest="col1" nameend="col2"\s*/>\s*</row>\s*
This regex will match the entire block including the newline characters. If you are using a programming language or a tool that supports multi-line regex, ensure you have the multi-line mode enabled.
Post Reply