Using RegExReplace() to match characters but not to replace/remove all matched characters

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
BeetleAHK2
Posts: 1
Joined: 02 Jun 2017, 11:26

Using RegExReplace() to match characters but not to replace/remove all matched characters

02 Jun 2017, 13:10

Hi. I'm wanting a very simple result but am tearing my hair out trying to figger out how to use RegExReplace() to do it. I am still using AHK (not AHK_L) but I don't think that makes a difference with RegExReplace(). I have no code-writing knowledge (except for BATch -- which is why I love AHK so much). So, here's my Haystack (quotes only used for this Forum post):
"REPLACED MB & LCD BACK COVER,DMI, UPDATED BIOS."

What I want to do is use RegExReplace() to add a space after the commas to give me this result:
"REPLACED MB & LCD BACK COVER, DMI, UPDATED BIOS."

Pretty simple result, right? Well, nothing I've tried has worked:

Code: Select all

Newclip = REPLACED MB & LCD BACK COVER,DMI, UPDATED BIOS.

Newclip := RegExReplace(Newclip, ",[^,\w]", ", ")
; The above does nothing.
Newclip := RegExReplace(Newclip, ",[^,\w]", ", \w")
; The above does nothing.
Newclip := RegExReplace(Newclip, ",[^,\w", ", \w")
; The above removes everything.
Newclip := RegExReplace(Newclip, ",\w", ", \w")
; The above gives "REPLACED MB & LCD BACK COVER, \wMI, UPDATED BIOS."
Newclip := RegExReplace(Newclip, ",\w", ", ")
; The above gives "REPLACED MB & LCD BACK COVER, MI, UPDATED BIOS."
Newclip := RegExReplace(Newclip, ",", ", ")
; The above gives "REPLACED MB & LCD BACK COVER, DMI,  UPDATED BIOS."
Basically, I want to match any comma that has a character (other than a space) directly following it and then replace JUST the comma with a comma and space (or add a space after the existing comma without replacing said comma).

Why oh WHY does this have to be so hard?!! Please help me learn RegEx!
Thanks in advance, Beetle.
phaleth
Posts: 38
Joined: 13 Apr 2015, 03:49

Re: Using RegExReplace() to match characters but not to replace/remove all matched characters

02 Jun 2017, 14:31

Code: Select all

Newclip = REPLACED MB & LCD BACK COVER,DMI, UPDATED BIOS.

Newclip := RegExReplace(Newclip, ",\s?", ", ")

MsgBox, % Newclip
lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: Using RegExReplace() to match characters but not to replace/remove all matched characters

02 Jun 2017, 16:34

To answer the original question, there are two ways to match characters but not replace them:
  1. A look-ahead (?=positive) or (?!negative) or look-behind (?<=positive) or (?<!negative) will verify that a pattern matches before or after the current position, without consuming any characters (i.e. the content matched by the assertion does not get replaced). For example, (?<=,)(?! ) or (?<=,)(?=[^,\w]) would insert the replacement text without removing any characters.
  2. \K resets the start position, so previously matched characters are not replaced.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Hansielein, Lpanatt and 322 guests