Jump to content

[solved] StringFormat(), where is the StringFormatBuilder() ?


Recommended Posts

StringFormat("\\\\server\\share") will return "\\server\share". That is nice but what about the other way around ?
Do I have to do StringReplace() each ( \t, \r, \n , etc. ) or is there a simpler way ?.  Thanks.

solved @ https://www.autoitscript.com/forum/topic/205642-solved-stringformat-where-is-the-stringformatbuilder/?do=findComment&comment=1479967

 

Edited by argumentum
solved =)

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

Func StringUnFormat($sText)
    Return StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace($sText, "\", "\\"), "%", "%%"), @CRLF, "\r\n"), @TAB, "\t"), @LF, "\n"), @CR, "\r")
EndFunc

Is this what you're looking for? I don't think there's another way to do it

Edit: Added \\ and %%

Edited by seadoggie01

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

You can't do that with a [single] regular expression, since a newline/tab can't be replaced with the respective escaped equivalent... right?

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

@seadoggie01, I can understand quantum entanglement, the reason for matter to exist, etc., but this darn Regular Expressions :wacko2:
I have no clue. A string in a variable is just a string.  

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

sorry for the intrusion, but it's only for a small question. Can you translate the nested string expression posted by @seadoggie01 in the second post into a single regexp pattern?
that is:
to change in the input string all the characters of the first group one by one with the corresponding characters of the second group?
example:
"Bread Whole Bull Ball", "aou", "eaa" -> should result in -> "Breed Whale Ball Bell"
explanation:
"Bread Whole Bull Ball": input string
"aou": characters to search for
"eaa": characters to replace
that is, replace all "a" with "e"; all "o" with "a" and all "u" with "a"
"Breed Whale Ball Bell": outgoing string

Thanks

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

@argumentum, do you mean something like this?

$sOut = "\\server\share\n\r\t" & @cr & @tab & "an%other"
$sIn = Execute("'" & StringRegExpReplace($sOut, "([\\%])|(\[nrt])", "' & '$1$1' & ('$2' ? '\\$2' : '') & '") & "'")
ConsoleWrite($sIn & @LF)
ConsoleWrite(StringFormat($sIn) & @LF)

Of course there's double cheating here: first I use two function calls, second it's impossible to "unformat" variables' values (which is what StringFormat is meant for in the first place).

EDIT: forget the completely idiotic code above!

; converting these:
; \ to \\
; % to %%
$sOut = "\\server\share\n\r\t" & @cr & @tab & "an%other"
$sIn = StringRegExpReplace($sOut, "([\\%])", "$1$1")
ConsoleWrite($sIn & @LF & @LF)
ConsoleWrite(StringFormat($sIn) & @LF)

But later posts reveal that you're after something else. See below.

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

;======================================================================
$str = "\\server\share\n\r\t" & @CRLF & @TAB & "an%other"
$s = StringRegExpReplace($str, '([\\%\r\t])', "$1$1")
ConsoleWrite('=== >' & $s & '< === this should be a one liner'& @CRLF)
#cs
=== >\\\\server\\share\\n\\r\\t

        an%%other< === this should be a one liner
#ce
;======================================================================
$sOut = "\\server\share\n\r\t" & @CRLF & @TAB & "an%other"
$sIn = Execute("'" & StringRegExpReplace($sOut, "([\\%])|(\[nrt])", "' & '$1$1' & ('$2' ? '\\$2' : '') & '") & "'")
ConsoleWrite('=== >' & $sIn & '< === this should be a one liner'& @CRLF)
#cs
=== >\\\\server\\share\\n\\r\\t
    an%%other< === this should be a one liner
#ce
;======================================================================

:(

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

My proposal above is terrible as was uselessly complicated, sorry.

If you insist on converting actual CRs, LFs & TABs, then:

; converting these:
; \ to \\
; % to %%
; newline to \n
; carriage return to \r
; tab to \t
$sOut = "\\server\share\n\r\t" & @cr & @tab & "an%other"
$sIn = Execute("'" & StringRegExpReplace($sOut, "([\\%]|\n|\r|\t)", "' & ('$1' = '\\' ? '\\\\' : '') & ('$1' = '%' ? '%%' : '') & ('$1' = @LF ? '\\n' : '') & ('$1' = @CR ? '\\r' : '') & ('$1' = @TAB ? '\\t' : '') & '") & "'")
ConsoleWrite($sIn & @LF & @LF)
ConsoleWrite(StringFormat($sIn) & @LF)

and the result is a one-liner.

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

I'm only half satisfied with that because it looks really heavy, but I've no magic wand in mind right now ... and may never have one.

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

11 hours ago, Chimp said:

to change in the input string all the characters of the first group one by one with the corresponding characters of the second group?

As there are multiple requirements I can't see how to avoid the use of an external container

$sd = ObjCreate("Scripting.Dictionary")
$sd.add("a", "e")
$sd.add("o", "a")
$sd.add("u", "a")

$str = "Bread Whole Bull Ball"
$r = Execute("'" & StringRegExpReplace($str, "(\w)", "' & ($sd.exists('$1') ? $sd.item('$1') : '$1') & '") & "'")
Msgbox(0,"", $r)

Edit : something simpler - but more limited

$s = "Bread Whole Bull Ball"
$in = "aou"
$out = "eaa"

$res = Execute('"' & StringRegExpReplace($s, '([aou])', '" & StringMid($out, StringInStr($in, "$1"), 1) &  "') & '"')
Msgbox(0,"", $res)

 

Edited by mikell
Link to comment
Share on other sites

Thanks Mikell, by simply changing Execute() to Consolewrite() I was able to see what that line of the first script does. Produces a single 851-character AutoIt instruction to parse every single letter of the input string. Brilliant and expensive at the same time; still interesting. so I can infer that there is no "internal" way to ask regexp to scan a sequence of characters and replace them with another set of substitutes.
Thanks a lot @mikell, always something interesting from you.
p.s. @argumentum, :ermm: sorry for my little hijacking. I'm leaving right now ... bye ;)

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

18 minutes ago, Chimp said:

so I can infer that there is no "internal" way to ask regexp to scan a sequence of characters and replace them with another set of substitutes.

That's simply because regexes (not only PCRE) are tools to match pieces of text but have no internal support for replacement, nor any other text processing. StringRegexReplace and similar features allow you to grab matched pieces from a subject and allow you to glue them verbatim at will, but there is no regex primitive to perform any transformation of the text pieces.

Furthermore, even if PCRE offers built-in If-Then-Else and even recursive constructs, none of them is available in the "replace pattern": it isn't a regular regex pattern, just a string with interpolation of sequences similar to regex back-references: $1,$2,... or \1,\2,... or ...$(17),$(18),...

This implies that applying any transformation to any piece of text output by the match phase has to be done by external code and --for a character translation-- a translation table in an external container, just as @mikell said.

In Perl regex have support for Perl interpolation, that is invoke Perl code when certain matching condition occurs, but it's still code external to regex which will be acting then.

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

×
×
  • Create New...