Replace multiple Regex occurences in a single line

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
steve88
Posts: 11
Joined: 11 Oct 2018, 04:43

Replace multiple Regex occurences in a single line

20 Apr 2021, 06:27

Hi!
I want to replace dots with commas within the string below (multiline option doesn't seems to be necessary). Pay attention to: 739.53;739.36:

Code: Select all

2332;2401;YY0940007777;007;2021/03/09;2021/03/09;48;"TEXT";43;C;739.53;739.36;"TEXT";"BRC:MRDRMX RRR: 00000RRRRRRRRYH (0432/T210) O/C TEXT; TEXT:TEXT YHBNDORBEGG                                                                                           YYY05552044                TEXT; TEXT                                                     ";"";
2332;2401;YY0940007777;007;2021/03/09;2021/03/09;48;"TEXT";43;C;739.53;739.36;"TEXT";"BRC:MRDRMX RRR: 00000RRRRRRRRYH (0432/T210) O/C TEXT; TEXT:TEXT YHBNDORBEGG                                                                                           YYY05552044                TEXT; TEXT                                                     ";"";
2332;2401;YY0940007777;007;2021/03/09;2021/03/09;48;"TEXT";43;C;739.53;739.36;"TEXT";"BRC:MRDRMX RRR: 00000RRRRRRRRYH (0432/T210) O/C TEXT; TEXT:TEXT YHBNDORBEGG                                                                                           YYY05552044                TEXT; TEXT                                                     ";"";
I wrote this regex command but it replaces only the first occurence for every line:

Code: Select all

csvContent := RegExReplace(csvContent,";(\d+)\.(\d+);",";$1,$2;")
thank you all!
Last edited by steve88 on 20 Apr 2021, 08:12, edited 1 time in total.
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: Replace multiple Regex occurences in a single line

20 Apr 2021, 08:10

You don't even need regex for this:

Code: Select all

csvContent := StrReplace(csvContent, ".", ",")
But it also works with regex:

Code: Select all

csvContent := RegExReplace(csvContent,";(\d+)\.(\d)",";$1,$2")
If you check for the semicolumn before and after the number, both semicolums are "used". The regex search starts again at the position behind the found string, and your regex uses the ";" between the two numbers for the first match, the second search starts after the semincolumn and thus does not fond a match anymore.
Last edited by braunbaer on 20 Apr 2021, 08:16, edited 1 time in total.
steve88
Posts: 11
Joined: 11 Oct 2018, 04:43

Re: Replace multiple Regex occurences in a single line

20 Apr 2021, 08:13

hi, thanks for the reply!
unfortunately I can't do this, because text fields can contain dots.
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: Replace multiple Regex occurences in a single line

20 Apr 2021, 08:18

Then use the regex that I added as alternative.
If that also does not work for you, use the look ahead assertion (?=;), which checks for the semicolumn to the right of the number without consuming it:

Code: Select all

csvContent := RegExReplace(csvContent,";(\d+)\.(\d+)(?=;)",";$1,$2")
steve88
Posts: 11
Joined: 11 Oct 2018, 04:43

Re: Replace multiple Regex occurences in a single line

20 Apr 2021, 08:50

thank you very much, you're great!

solution:

Code: Select all

csvContent := RegExReplace(csvContent,"(\\d+)\\.(\\d+)\\;?","$1,$2;")

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 214 guests