Jump to content

[SOLVED] StringRegExpReplace()


Recommended Posts

Local $sVar = 'fsdf\\x\\dsadsad\das\/'
ConsoleWrite($sVar & @CRLF )
Consolewrite(StringRegExpReplace($sVar, '[^(\\.)]', ' '))

Hello forums,

How do I replace with spaces all characters except for those that match the regexp \\. (which matches backslash followed by any character).

I've tried the above code but it doesn't work properly.

Thank you!

Edit1: Please see posts #7 and 8 for more details.

Edit2: Solved!
See Post#9 by jchd. Add #include <string.au3> to the code.

Edited by Mingre
Add 'solved' to title.
Link to comment
Share on other sites

Like this:

Local $sVar = 'fsdf\\x\\dsadsad\das\/'
ConsoleWrite($sVar & @LF)
Consolewrite(StringRegExpReplace($sVar, '(\\.)', ' ') & @LF)

 

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

Note that your current regexp means:

  • /[^(\\.)]/
    •  
      [^(\\.)] match a single character not present in the list below
      • ( the literal character (
      • \\ matches the character \ literally
      • .) a single character in the list .) literally

(I used regex101.com.)

Note that if you reformulate your question, you end up with a requirement of changing any character that does not match a combination of two characters. That doesn't fit.

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Local $sText= 'fsdf\\x\\dsadsad\das\/'
Local $sVar = 'fsdf\\x\\dsadsad\das\/'
ConsoleWrite($sVar & @CRLF )
$x = StringRegExp($sVar, '(\\.)',3)
Local $sNew = '', $iCount
For $element In $x
    $iCount = StringInStr($sVar, $element)
    ;ConsoleWrite($element & ' <' & $iCount & '>' & @LF)
    $sVar = StringTrimLeft($sVar, $iCount + 1)
    ;MsgBox(0, $sText, $sVar)
    $sNew &= StringFormat('%' & $iCount + 1 & 's', $element)
NExt
ConsoleWrite($sNew & @LF)

Hello again, basically what I wanted to get is a shorter version of the above code.

Link to comment
Share on other sites

So basically you want to replace any character that is not part of the regex \\. unless it is also a combination of the regex \\\\. ? Because you want to keep the d in \d, but not the x in \\x or the d in \\d.

Hmmz... I wouldn't know a regex for that. Hopefully someone else does.

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Sorry I misread the OP. Try this:

Local $sVar = 'fsdf\\x\\dsadsad\das\/xyz\u\w'
ConsoleWrite($sVar & @LF)
Local $sOut = Execute(StringRegExpReplace($sVar, '([^\\]+)((?:\\.)*)', '_StringRepeat(" ", StringLen("$1")) & "$2" & ') & '""')
ConsoleWrite($sOut & @LF)

 

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

No.

2015-07-25_121741.png

Edited by jchd

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

Looks like you miss this:

#include <String.au3>

 

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

jc, is this one good ?

Local $sOut = StringRegExpReplace($sVar, '(?<=^|\\\\|[^\\])[^\\]', " ")

 

Hello mikell, yours works perfectly too. And I like its simplicity :D

Would you be so kind to explain how the first '^' works? I don't quite get it. Thanks!

Edit1: Nevermind, just found out how it works :P

Edit2:

Apparently,while testing both codes, they didn't work well when the string tested was '\\\a' (which turned '\\\ ')

Edited by Mingre
Link to comment
Share on other sites

Ah, sorry escape at the head need this:

#include <String.au3>
Local $sVar = '\\\a\bdfsdf\\x\\dsadsad\das\/xyz\u\w'
ConsoleWrite($sVar & @LF)
Local $sOut = Execute(StringRegExpReplace($sVar, '([^\\]*?)((?:\\.)*)', '_StringRepeat(" ", StringLen("$1")) & "$2" & ') & '""')
ConsoleWrite($sOut & @LF)

 

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

#include <String.au3>

$sVar = "000\a\aa\\bbb\\\ccc\\\\ddd\\\\\eee\\\\\\fff\\\\\\\ggg"
;expected result:   ...\a\a.\\...\\\c..\\\\....\\\\\e..\\\\\\...\\\\\\\\g..

Local $sOut1 = Execute(StringRegExpReplace($sVar, '([^\\]*?)((?:\\.)*)', '_StringRepeat(".", StringLen("$1")) & "$2" & ') & '""')

Local $sOut2 = StringRegExpReplace($sVar, '(((?<=^|[^\\])(?:\\\\)+)|()(?<=^|[^\\]))[^\\]', "$2.")

Msgbox(0,"", $sVar & @crlf & $sOut1 & @crlf& $sOut2)

 

Edited by mikell
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...