gruntydatsun Posted November 12, 2018 Posted November 12, 2018 I have an XML file and every time there are three lines in a row with only <null/> in them, i want to insert a fourth line with <null/>. Each line starts with 3 white spaces, followed by <null/> and ends with a white space followed by CR LF. The presence of the three lines as described is unique to the points where I want to insert a line in this document. I'm trying to figure out how to apply the repeating part of a regex {1,4} but apply it to this whole segment. So far I have the below which picks up an individual line ok: ^\s{3}<null/>\s\r\n I tried wrapping it all in braces () then adding {3} but I'm obviously getting something wrong. Attached is a section from the xml file with a block of nulls that should be matched if anyone would like to have a look. Help_From_Forum.xml
TheXman Posted November 12, 2018 Posted November 12, 2018 @gruntydatsun Here's one of several ways you could do it. example() Func example() Local $sBefore = _ '<row name="subTotal" mergeCellEnd="7" mergeCellStart="1"> ' & @CRLF & _ ' <cell type="text" name="subTotal">Subtotal: Hotpants Sales To Senior Execs</cell> ' & @CRLF & _ ' <null/> ' & @CRLF & _ ' <null/> ' & @CRLF & _ ' <null/> ' & @CRLF & _ ' <cell type="number" name="Year 1">0</cell> ' & @CRLF & _ ' <cell type="number" name="Year 2">0</cell> ' & @CRLF & _ ' <cell type="number" name="Year 3 and 4">1,000</cell> ' & @CRLF & _ ' <null/> ' & @CRLF & _ ' <null/> ' & @CRLF & _ '</row> ' & @CRLF Local $sAfter = StringRegExpReplace($sBefore, "(?sm)((?:^\s{3}<null/>\s\r\n){3})", "\1 <null/> " & @CRLF) ConsoleWrite("Before:" & @CRLF) ConsoleWrite($sBefore & @CRLF) ConsoleWrite("After:" & @CRLF) ConsoleWrite($sAfter & @CRLF) EndFunc gruntydatsun 1 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman
mikell Posted November 12, 2018 Posted November 12, 2018 (edited) 6 hours ago, gruntydatsun said: I tried wrapping it all in braces () then adding {3} ... and there is nothing more to do but add the \K ("keep out") verb $sAfter = StringRegExpReplace($sBefore, "(?m)(^\s{3}<null/>\s\r\n){3}\K", "$1") Edit Please note that as \K is used here as a lookbehind alternative, obviously this works too $sAfter = StringRegExpReplace($sBefore, "(?m)(?<=(^\s{3}<null/>\s\r\n){3})", "$1") Edited November 12, 2018 by mikell FrancescoDiMuro and gruntydatsun 2
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now