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

    • gruntydatsun
      By gruntydatsun
      I have an XML file and every time there are three lines in a row with only <null/> in them, i want to insert a fourth line with <null/>.   Each line starts with 3 white spaces, followed by <null/> and ends with a white space followed by CR LF.   The presence of the three lines as described is unique to the points where I want to insert a line in this document.
       I'm trying to figure out how to apply the repeating part of a regex  {1,4} but apply it to this whole segment. 
      So far I have the below which picks up an individual line ok:
      ^\s{3}<null/>\s\r\n I tried wrapping it all in braces () then adding {3} but I'm obviously getting something wrong. 
      Attached is a section from the xml file with a block of nulls that should be matched if anyone would like to have a look.
      Help_From_Forum.xml
    • nooneclose
      By nooneclose
      I need help turning this string "20180913221626" into a formatted time string.
      I need to go from this: 20180913221626
      to this: 09/13/2018 10:16 PM
      I do not always know what the date will be so I can not just use a variable I need to actually convert/format. 
      I did see an older post in the forms that was basically the same question only the other guy did not post the working code and I can not figure out how to use _AD_GetObjectProperties properly to get what I want. 
      As always any help would be appreciated. 
      Here is the code I use to find the date, but it always gives me the unformatted version. 
      ;retrieve the items object $oItem = $oOutlook.Session.GetItemFromID($aItems[1][0], Default) $oItem.GetInspector $eSentOn = $oItem.SentOn ; When was the email sent? MsgBox("", "Sent On of the email", $eSentOn) ;******************************************************************************* ; Formats the date and time from the email ;******************************************************************************* ;Local $fDatenTime = _DateTimeFormat($eSentOn, 1) ;MsgBox("", "Formatted email time", $fDatenTime) $aProperties = _AD_GetObjectProperties($eSentOn) _ArrayDisplay($aProperties, "Did the conversion work?")  
    • Miliardsto
      By Miliardsto
      I got that func
      Func makeHelpImgGUI($title,$width,$height,$img) $img = GUICtrlCreatePic("",20,40,$width,$height) _ResourceSetImageToCtrl($img, "HERE") EndFunc and I call this func like that
      makeHelpImgGUI("Image",1190, 800,$SETTINGS_JPG)  
      so what is the problem in the parameter where is - "HERE" I need value of img but passed as string
      so $img = $SETTINGS_JPG and how make it "SETTINGS_JPG"
       
      I tried something like that but not work
      Func makeHelpImgGUI($title,$width,$height,$img) $name_str = String($img) $name_str = StringTrimLeft ($name_str, 1 ) $img = GUICtrlCreatePic("",20,40,$width,$height) _ResourceSetImageToCtrl($img, $name_str) EndFunc  
    • liagason
      By liagason
      Hello everyone,
      How can I display in ascending  sequence some numbers stored in a string variable?
      $str = "18,03,48,23" MsgBox(0,"test",$str) I would like it to display "03,18,23,48"
    • 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
       
×