Jump to content

Need help with StringRegExpReplace


Recommended Posts

I feel what I'm trying to do is simple, though I can't even get a somewhat functional script going to do it... I have an xml file with tags enclosed as normal <tag> but in several places of several files I have some that are on multiple lines, such as:

<tag
attrib="0"
attrib="1"
>

I need to remove the CR and LF from this to give me:

<tag attrib="0" attrib="1">

I do NOT want to remove ALL CR and LF characters, only those within tags. I would expect what I want to do is find a pattern of <*> where * can be anything, then within * I need to replace any chr(10) or chr(13) with "". Now, saying isn't doing. Here's my latest NONWORKING try at it. I thought it would find < then anything then CR or LF, then anything then > and replace the same but without any CR or LF betweek the < and > Any Suggestions?

Local $read = FileRead("ReplaceTestIn.xml")
$Search = '[<](.*?)([\n|\r])(.*?)[>]'
$Replace = '<${1}${3}>'
$read = StringRegExpReplace($read, $Search,$Replace)
FileWrite("ReplaceTestOut.xml",$read)
Link to comment
Share on other sites

When there is no match, there is no replacement so you get the input subject verbatim as result.

I don't have off-hand a clean one-liner solution for the general problem you submit but you may try something like

$pattern = "(?x) ([^<]*) (<) (.*) (s) (.*) (s) (.*) (s) (.*) (s) (.*) (s) (>) ([^<]*)"
$repl = "$1$2$3$5$7$9$11$13$14"

Note: the x option (?x) permits non-significant whitespaces in the pattern so that it's more readable.

I confess it's not clever but it will process the following input correctly:

Place random
text
bafore<tag
attrib="0"
attrib="1"
> some
text
after

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

This method uses StringRegExpReplace to capture the text between "<tag" and ">", then uses a nested StringRegExpReplace to remove all CR and LF characters from the captured text only.

#cs
    ----- Contents of "TestTag.txt" file -------
    Place random
    text
    before<tag
    attrib="0"
    attrib="1"
    > some
    text
    after
#ce

$pattern = "(?s)(^.*<\s*tag)(.*?)(>.*$)"
$repl = "$1 "" & StringRegExpReplace('$2','(\\v+)', '') & ""$3"
;Space here^, could change to @LF, and no space here ^ between attributes, could change to a space (referencing the line above).

;ConsoleWrite('"' & StringRegExpReplace(FileRead("TestTag.txt"), $pattern, $repl) & '"' & @LF)
ConsoleWrite(Execute('"' & StringRegExpReplace(FileRead("TestTag.txt"), $pattern, $repl) & '"') & @LF)

#cs
    Returns:-
    Place random
    text
    bafore<tag attrib="0"attrib="1"> some
    text
    after
#ce
Link to comment
Share on other sites

:oops:

Hmm, I'd call that a cheating one-liner!

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

After spending nearly 30 minutes to understand how the "nesting" example works

I believe that ,at least, in the way it is written it does only one substitution (one tag)

I added one more tag to the testtag file and I got nothing

I would do it like this

#cs
Contents of testtag.txt
<td
valign="top"
align="Left">
<b><font
size="2"><font
color="white"
>Greek
</font></font></b>
style="float: left;
padding-top: 2px;"></span> <input
value="Display"
style="margin: 0px;
padding: 0px;
width: 80px;
font-size: 10px;"
type="button">
    Place random
    text
    before<tag
    attrib="0"
    attrib="1"
    > some
    text
    after
#ce

$txt = FileRead ("testtag.txt")
$tag = StringRegExp ($txt, "(?s)<.*?>", 3)
For $i = 0 To UBound ($tag) - 1
    $txt = StringReplace ( $txt, $tag[$i], StringRegExpReplace ($tag [$i], " *rn *", " "))
Next
MsgBox (0, "", $txt)
Exit

#cs
    Returns:-
<td valign="top" align="Left">
<b><font size="2"><font color="white" >Greek
</font></font></b>
style="float: left;
padding-top: 2px;"></span> <input value="Display" style="margin: 0px; padding: 0px; width: 80px; font-size: 10px;" type="button">
    Place random
    text
    before<tag attrib="0" attrib="1" > some
    text
    after
#ce

It does leave one blank before the ">" if it is on the start of the line, but maybe somebody else can fix this

Edited by hawkair
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...