Jump to content
Sign in to follow this  
Realm

[Solved] swapping characters

Recommended Posts

Realm

I will try to explain what I need best I can,

lets say

$string = "Hello @visitor, last time you visited us, you were reading @page, what topic would you like to read about today?"

I need to convert this string to

$string = "Hello" & $visitor & ", last time you visited us, you were reading" & $page & "what topic would you like to read about today?"

I was looking at StringRegExpReplace, but could not figure out how to get it the way i need it

I'm reading the first string from a txt file, so if it is easier, or possible, to modify the txt file first, that is ok too.

NVM I found a fix, but is there a better way than this?

$string = "Hello @visitor, last time you visited us, you were reading @page, what topic would you like to read about today?"
$string = StringReplace($string, '@visitor', ' " & $visitor & "')
$string = StringReplace($string, '@page', ' " & $page & "')
MsgBox(0, "New string is", $string)
Edited by Realm

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites
Melba23

Realm,

My attempt at an SRE: :idea:

$sText = '"Hello @visitor, last time you visited us, you were reading @page, what topic would you like to read about today"'

$sNewtext = StringRegExpReplace(StringRegExpReplace($sText, "(?i)(?U)(\x20@visitor)", '" & $visitor & "'), "(?i)(?U)(\x20@page)", '" & $page & "')

MsgBox(0, "Changed", $sNewtext)

OK, I see you have found a way with StringReplace - normally SREs are much faster than the String* functions, but a lot depends on the size of your file. :)

And whichever way you choose, I would recommend reading in the whole file, changing the text and then rewriting it. Doing it line-by-line will probably take longer, and be more error-prone.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Fulano

Here's my take on it (I lifted everything but the regex from Melba :idea:)

$sText = '"Hello @visitor, last time you visited us, you were reading @page, what topic would you like to read about today"' 
$sNewtext = StringRegExpReplace($sText, "@([A-z0-9]+)", '" & $\1 & "')
MsgBox(0, "Changed", $sNewtext)

#fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!

Share this post


Link to post
Share on other sites
Realm

Ok I tried this, but its not working, what am I doing wrong?

this is 3 samples from my ini

[Messages]

welcome=Hello @visitor, last time you visited us, you were reading @page, what topic would you like to read about today?

thanks=Thank you for buying @book. @visitor, Come back anytime we are open 24/7.

buybalmsg=@visitor, your balance is: @buynumber Gold.

this is a snippet i coded to just read them and see how it works, but it don't

$var = IniReadSection("C:\Temp\myfile.ini", "Messages")
If @error Then
    MsgBox(4096, "", "Error occurred, probably no INI file."
Else
    $sNewtext = StringRegExpReplace(StringRegExpReplace($var, "(?i)(?U)(\x20@visitor)", '" & $visitor & "'), "(?i)(?U)(\x20@page)", '" & $page & "', & _
    & "(?i)(?U)(\x20@book)", '" & $book & "', "(?i)(?U)(\x20@buynumber)", '" & $buynumber & "')
    For $i = 1 To $sNewtext[0][0]
    MsgBox(4096, "", "Key: " & $sNewtext[$i][0] & @CRLF & "Value: " & $sNewtext[$i][1])
    Next
EndIf

What I ultimately need this to do is to read all the messages into Global variables, so they can be called randomly throught the script. The Global variables are the same as the in keys such as ini file key 'Welcome' = Global variable $welcome

Sorry the error i get is in the StringRegExpReplace, says error in expresion

Edited by Realm

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites
Melba23

Fulano,

I am so glad there are people around here who actually understand these blasted things! :idea:

As I have said many times before (and no doubt will many times more) these RegExes are the hardest thing I have ever tried to learn*.

M23

* With regard to computers, of course. :)


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Realm

Fulano,

I am so glad there are people around here who actually understand these blasted things! :idea:

As I have said many times before (and no doubt will many times more) these RegExes are the hardest thing I have ever tried to learn*.

M23

* With regard to computers, of course. :)

I Second that motion


My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites
Melba23

Realm,

Our posts crossed.

You cannot just add different parts to the StrngRegExpReplace like that. :idea:

I see you are using an ini file - that simplifies things a bit. So, stealing from Fulano (I just know he will not mind! :) ):

$sIniFile = "Realm.ini" ; this is the text you gave us above

$aLines = IniReadSection($sIniFile, "Messages")

For $i = 1 To $aLines[0][0]

    $aLines[$i][1] = StringRegExpReplace($aLines[$i][1], "@([A-z0-9]+)", '" & $\1 & "')

Next

IniWriteSection($sIniFile, "Messages", $aLines)

I think that will do the trick. :(

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Fulano

Np, just takes a special kind of crazy Posted Image


#fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!

Share this post


Link to post
Share on other sites
Realm

Fulano, thanks, I got it now

Global $welcome = IniRead("C:\Temp\myfile.ini", "Messages", "welcome", "NotFound")
Global $thanks = IniRead("C:\Temp\myfile.ini", "Messages", "thanks", "NotFound")
Global $buybalmsg = IniRead("C:\Temp\myfile.ini", "Messages", "welcome", "NotFound")

$sNewtext = StringRegExpReplace($welcome , "@([A-z0-9]+)", '" & $\1 & "')
MsgBox(0, "Changed", $sNewtext)

$sNewtext = StringRegExpReplace($thanks , "@([A-z0-9]+)", '" & $\1 & "')
MsgBox(0, "Changed", $sNewtext)

$sNewtext = StringRegExpReplace($buybalmsg , "@([A-z0-9]+)", '" & $\1 & "')
MsgBox(0, "Changed", $sNewtext)

works like a charm, and not as messy as i thought it would end up


My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites
Melba23

Realm,

But you have not seen my last post yet! :idea:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
jchd

AutoItSetOption('ExpandVarStrings', 1)

It won't work with @xyz (reserved for macros) but will work for (flat) variables.

$visitor = 'Realm'

$page = 'http://www.autoitscript.com/autoit3/docs/functions/AutoItSetOption.htm'

$string = "Hello $visitor, last time you visited us, you were reading $page what topic would you like to read about today?"

will evaluate the way you whish without fuss.


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
Spiff59

I wonder if you might be better off using something like "@@" as your keyword delimiter in case you ever want to include an actual "@" in the text of your messages (for instance, an email address).

Edited by Spiff59

Share this post


Link to post
Share on other sites
Fulano

I wonder if you might be better off using something like "@@" as your keyword delimiter in case you ever want to include an actual "@" in the text of your messages (for instance, an email address).

Good point!

#fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!

Share this post


Link to post
Share on other sites
Realm

Realm,

Our posts crossed.

You cannot just add different parts to the StrngRegExpReplace like that. :idea:

I see you are using an ini file - that simplifies things a bit. So, stealing from Fulano (I just know he will not mind! :) ):

$sIniFile = "Realm.ini" ; this is the text you gave us above

$aLines = IniReadSection($sIniFile, "Messages")

For $i = 1 To $aLines[0][0]

    $aLines[$i][1] = StringRegExpReplace($aLines[$i][1], "@([A-z0-9]+)", '" & $\1 & "')

Next

IniWriteSection($sIniFile, "Messages", $aLines)

I think that will do the trick. :(

M23

Your Right I did'nt see it, I tested it, I like it, but I will change the iniwrite to another file location/name, so that it does not conflict with the config script that modifies it

Thanks


My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites
jchd

Are you bound with @ character or can you use the ExpandVars option?


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
Realm

I wonder if you might be better off using something like "@@" as your keyword delimiter in case you ever want to include an actual "@" in the text of your messages (for instance, an email address).

That is a wonderful suggestion, thanks


My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites
Realm

Are you bound with @ character or can you use the ExpandVars option?

Unfortunately from which the information comes is declared only with @variables.

I'm trying to keep it simple for the game module I'm creating for a teacher, which will allow her students, end users, to make some of the game themselves using allready declared variables, she wants them to be easy like @variable.

Unless you have a better idea that will keep it simple for her elementary level students?

Edited by Realm

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites
jchd

Then can you replace @ by $ and name your variables accordingly. The expansion will be automatic with this option..


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
Realm

Then can you replace @ by $ and name your variables accordingly. The expansion will be automatic with this option..

Unfortunately the teacher hired someone to create this entire job. they wrote a working config file, but the game module does not work at all. the config file is compiled, so I would have to write a custom config file for here, and still have to reformat the string.

$string = "Hello $visitor, last time you visited us, you were reading $page what topic would you like to read about today?"

$string = "Hello" & $visitor & ", last time you visited us, you were reading" & $page & "what topic would you like to read about today?"

At this point seems, easier to just work the game module to reformat the current output which inclueds the @variables format.

I do appreciate the suggestion, it would be better and easier, if I could modify the config file

Edited by Realm

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites
Spiff59

At this point seems, easier to just work the game module to reformat the current output which inclueds the @variables format.

I do appreciate the suggestion, it would be better and easier, if I could modify the config file

If you're stuck with this format, then I might add a space to each side of the StringRegExReplace(), so that you are matching on " @" instead of just "@", that would make for a more bullet-proof script that would permit strings like "me@home.com" to exist in the text of the message.

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
Sign in to follow this  

×