Jump to content

Using StringRegExpReplace To Replace Section of a File


Recommended Posts

I'm trying to use StringRegExpReplace to replace/remove a section of a file that includes carriage returns.

File contents:

interface Vlan2
ip address 192.168.21.1 255.255.255.0
##DBB##
ip nat inside
no shut
exit
##DBB##

Expression:

StringRegExpReplace($chars, "##DBB##(.*?)##DBB##", "")

In this case, I'd like to replace anything in between the two delimiters "##DBB##" with an empty "". However, (.*?) doesn't play nicely with carriage returns. I've tried variations of \v and \s to no avail. Can someone let me know what else I need to try?

Link to comment
Share on other sites

You want: "(?s)##DBB##.*##DBB##"

Modifier "(?s)" specifies that newlines also match ".".

:mellow:

P.S. If you want to keep the delimiters:

$sChars = StringRegExpReplace($sChars, "(?s)(##DBB##)(.*)(##DBB##)", "\1" & @CRLF & "\3")

:P

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

You want: "(?s)##DBB##.*##DBB##"

Modifier "(?s)" specifies that newlines also match ".".

Thanks! Please help me understand why (?s) is added to the beginning of the delimiter. I was experimenting with ?s and \v but I was placing them everywhere except the beginning...

Link to comment
Share on other sites

That's a control switch, changing how the rest of the pattern is interpreted. It doesn't match or return anything on its own. In this case it changes how "." is interpreted from that point forward.

Another example is "(?i)" for case insensitive.

:mellow:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Beside the brief introduction found in AutoIt helpfile, you can refer to here and here as reference documentations.

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

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...