Mingre

[SOLVED] StringRegExpReplace()

22 posts in this topic

#1 ·  Posted (edited)

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.

Share this post


Link to post
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)

Share this post


Link to post
Share on other sites

Thanks jchd for the response! What I'd like to do tho is to replace only those that do not match the regexp (\\.)

Share this post


Link to post
Share on other sites

Can you specify what you would expect exactly (including leading/trailing spaces etc.) to be the output of the example?


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

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Hello sadbunny, sorry for not being clear.

I would like line 1 to turn into line 2 with just stringregexpreplace. 

fsdf\\x\\dsadsad\das\/
    \\ \\       \d  \/

 

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

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.

Share this post


Link to post
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)

Share this post


Link to post
Share on other sites

SadBunny, no prob. Thank you anyway!

jhcd, it returns an empty string.

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

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)

Share this post


Link to post
Share on other sites

Sorry but It does in my PC. :o (Please see attached file.)

empty.bmp

Share this post


Link to post
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)

Share this post


Link to post
Share on other sites

Looks like you miss this:

#include <String.au3>

 

It works perfectly now! Thanks!

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

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

What should be the return for '\\\a' ?

Share this post


Link to post
Share on other sites

\\\a should still return \\\a, but both codes return '\\\ '.

Share this post


Link to post
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)

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

#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

Share this post


Link to post
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