Jump to content

StringRegExpReplace - replace 'COMMA' with ','


Recommended Posts

Hi All,

I'd like to replace 'COMMA' with ',' for example:

$myString = "COMMA"
StringRegExpReplace($myString, 'COMMA', ',')

Now I've tried escaping the ',' in various ways unsuccessfully, such as:
'[,]'
"[,]"
'\,'

[,] seems to work in the pattern, I just can't figure out how to use it in the replace, and it seems everyone online is only interested in removing/replacing commas lol.

I also tried creating and using a variable as the replacement but also didn't work:

$myComma = ","
$myString = "COMMA"
StringRegExpReplace($myString, 'COMMA', $myComma)

I'm sure it's super simple if someone could point me in the right direction - thanks.

Link to comment
Share on other sites

Hi.

This works for me:

Local $sMyString = "HelloCOMMA this is a testCOMMA if Comma will be replaced."
Local $sReplacedString = StringRegExpReplace($sMyString, 'COMMA', ',')
ConsoleWrite($sReplacedString & @CRLF)

Regards, Conrad

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Link to comment
Share on other sites

Thanks guys for your replies, I've realised my issue is not in the replacement, it's later when I split out the string into an array... it's actually working which is why I'm getting empty elements. So I'll have to fix this by changing the separator value.

Thanks again guys for clarifying it's not the escaping that's the issue.

Link to comment
Share on other sites

Can you provide an accurate reproducer of just what you are starting with, and what you want it to look like in the end. 

If the ultimate issue is a split leaving empty elements, and we have easily identifiable characters to split on, then regex may be unnecessary.  (e.g. things like stringsplit can split on entire substrings). 

#include<array.au3>

$myString = "SomeTextCOMMAMoarTextCOMMAThisCSVisWeirdCOMMAEndOfText"

_ArrayDisplay(stringsplit($myString , "COMMA" , 3))

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

All good thanks @iamtheky, I need to split using a "," as the delimiter for everything else. I walk through the array after my StringRegExpReplace to make a 2D array anyway, so I just ran an IF statement while walking through the array to replace any instance of COMMA with an actual ",". Cheers.

Link to comment
Share on other sites

Here's a follow on question though, when I am walking through the array, would it be more efficient to (assuming < 10 instances of "COMMA" in the array):

  1. Use StringInStr on each array element and if it finds "COMMA" in the element to then do a StringRegExReplace (this is what I'm currently doing), or
  2. Just run a StringRegExReplace on every element

It's neither here nor there in this case as the 2D array has less than 1,000 elements anyway, but I'm interested in the concept.

Link to comment
Share on other sites

I'm sure you will reach a threshold between 'total elements searched -vs- how many matches' where you could optimize.  You could _arraytostring and then just run a single regular expression, dont know if it beats a loop though.

Or you could grab only the matching elements out of the array first with UDFs that are already optimized, then act only on the matches with native functions.  That usually ends up cleaner and quicker.

#include<array.au3>

local $arr = ["SomeTextCOMMA" , "MoarTextCOMMA" , "TextAboutASitcomMaybe" , "AndAnotherCOMMA" , "EndOfText"]

$found = _ArrayFindAll($arr , "COMMA" , 0 , 0 , 1 , 1)

For $i = 0 to ubound($found) - 1
    $arr[$found[$i]] = stringreplace($arr[$found[$i]] , "COMMA" , ",")
Next

_ArrayDisplay($arr)

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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