Jump to content
WoodGrain

StringRegExpReplace - replace 'COMMA' with ','

Recommended Posts

WoodGrain

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.

Share this post


Link to post
Share on other sites
benners

Use StringReplace instead

Share this post


Link to post
Share on other sites
Simpel

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 = Win7Pro SP1   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.

Share this post


Link to post
Share on other sites
WoodGrain

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.

Share this post


Link to post
Share on other sites
WoodGrain

Alternatively I could leave "COMMA" and look at changing the affected elements after I've created the array.

Share this post


Link to post
Share on other sites
iamtheky

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

 


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

Share this post


Link to post
Share on other sites
WoodGrain

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.

Share this post


Link to post
Share on other sites
WoodGrain

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.

Share this post


Link to post
Share on other sites
iamtheky
Posted (edited)

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

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

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

  • Similar Content

    • Rskm
      By Rskm
      Hi, I have the following line in a text file 'input.txt'. I know the line number - say '6'. I wish to replace the text 'WWW' in the below line with a random number (I can generate that with random()).
      WERIS  WWWJP   3.83  8.330  1.000                1097.RAXX 
      The WWW is a 3 digit integer (could be any number between 0 to 999), I can use stringtrimleft and get the numerical value of WWW in this file
      so, basically, I know the string to replace (ie; WWW stored in a variable), I know the line number to work on and the file location/name and the replacement variable (through random()). My requirement is to fill that 3 spaces with my random number (which Is a integer between 1 and 999)
      please put ur suggestions
       
    • milkmoron
      By milkmoron
      I am trying to search in a web browser dates XX/XX/XXXX that are also links. I want to click them after and remove them from the array. This is all I have so far. Nothing shows up. What am I doing wrong?
      ControlFocus ("Customer Center", "", "")
      Local $aArray = StringRegExp('(..)/(..)/(....)', '(..)/(..)/(....)', $STR_REGEXPARRAYFULLMATCH)
      For $i = 0 To UBound($aArray) - 1
          MsgBox($MB_SYSTEMMODAL, "RegExp Test with Option 2 - " & $i, $aArray[$i])
      Next
       
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good evening everyone
      I am working with Word UDF ( thanks @water! ), and, especially, with the function _Word_DocFindReplace().
      The replace does work everywhere in the document, but, it does not work in Headers or Footers.
      Am I missing something or am I forced to use the code below?
      I have already looked in the Help file ( about _Word_DocFindReplace() ), but there are no mentions about replace text in Headers/Footers.
      Sub FindAndReplaceFirstStoryOfEachType() Dim rngStory As Range For Each rngStory In ActiveDocument.StoryRanges With rngStory.Find .Text = "find text" .Replacement.Text = "I'm found .Wrap = wdFindContinue .Execute Replace:=wdReplaceAll End With Next rngStory End Sub Thanks everyone in advance


      Best Regards.
    • nacerbaaziz
      By nacerbaaziz
      Hello Members of this best Forum
      i have a question please
      for example if i have a long string
      and i want to extract a text between two tag
      what i can do to make that?
      note :
      i know that there is a
      StringRegExp function
      it's do that work
      but it result is be as an array
      i want the result to be a string
      is there any function on autoit can do that?
      Thanks in advance.
×