RegExReplace - add many options

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Albireo
Posts: 1756
Joined: 16 Oct 2013, 13:53

RegExReplace - add many options

19 Feb 2018, 10:14

Hi!
Have looked at RegExReplace() and tried to find the function I'm looking for.
Tried step by step, until it became an acceptable result. (Tried to use (* ANYCRLF) but get stuck.)
Here is a result .:

Code: Select all

#SingleInstance	force
#NoEnv

vText = " `t This     `nis, "only" a  small  `ntest.     "
Str := RegExReplace(vText, "^\""", "")	; Remove "" in the beginning
Str := RegExReplace(Str, "\""$", "")	; Remove "" at the end
Str := RegExReplace(Str, "`n")	; Remove all newlines
; Str := RegExReplace(Str, "`r")	; Remove all newlines
Str1 := RegExReplace(Str, "^ +", "")	; Remove spaces in the beginning
Str1 := RegExReplace(Str1, " +$", "")	; Remove spaces at the end
Str2 := RegExReplace(Str1, "`t", "")	; Remove all tabs
Str3 := RegExReplace(Str2, "\x20{2,}", " ")	; keep only one space between the words
MsgBox 64, Row.: %A_LineNumber% -> %A_ScriptName%, % "vText .: -" vText "-`n`nStr .: -" Str "-`nStr1 .: -" Str1 "-`nStr2 .: -" Str2 "-`nStr3 .: -" Str3 "-"

ExitApp
How to add options together in RegExReplace?
Rohwedder
Posts: 7644
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: RegExReplace - add many options

19 Feb 2018, 14:22

Hallo,
certainly not the optimum!

Code: Select all

#SingleInstance	force
#NoEnv

vText = " `t This     `nis, "only" a  small  `ntest.     "
Str := RegexReplace(vText, "[`r`n`t]","") ;remove newlines and tabs
Str1 := Trim(Str," """) ; Remove "" and spaces in the beginning and at the end
Str2 := RegExReplace(Str1, "\x20{2,}", " ")	; keep only one space between the words
MsgBox 64, Row.: %A_LineNumber% -> %A_ScriptName%, % "vText .: -" vText "-`n`nStr .: -" Str "-`nStr1 .: -" Str1 "-`nStr2 .: -" Str2 "-"

ExitApp
teadrinker
Posts: 4330
Joined: 29 Mar 2015, 09:41
Contact:

Re: RegExReplace - add many options

19 Feb 2018, 15:00

Hi!

Code: Select all

vText = " `t This     `nis, "only" a  small  `ntest.     "
MsgBox, % "|" RegExReplace(vText, "(^""\s+|\s+""$)|(?<=\S\s)\s+") "|"
Albireo
Posts: 1756
Joined: 16 Oct 2013, 13:53

Re: RegExReplace - add many options

19 Feb 2018, 17:02

Thanks!
Rohwedder wrote:......

Code: Select all

#SingleInstance	force
#NoEnv

vText = " `t This     `nis, "only" a  small  `ntest.     "
Str := RegexReplace(vText, "[`r`n`t]","") ;remove newlines and tabs
Str1 := Trim(Str," """) ; Remove "" and spaces in the beginning and at the end
Str2 := RegExReplace(Str1, "\x20{2,}", " ")	; keep only one space between the words
MsgBox 64, Row.: %A_LineNumber% -> %A_ScriptName%, % "vText .: -" vText "-`n`nStr .: -" Str "-`nStr1 .: -" Str1 "-`nStr2 .: -" Str2 "-"
Maybe not the optimum, but I understand what happen, and it works. (and how to do)
teadrinker wrote:...

Code: Select all

vText = " `t This     `nis, "only" a  small  `ntest.     "
MsgBox, % "|" RegExReplace(vText, "(^""\s+|\s+""$)|(?<=\S\s)\s+") "|"
This is much more difficult to understand. But it works!
I will try to understand how this command should be translated / interpreted. (Is there anyone who can help me to translate?)
Have no idea what solution is faster.
teadrinker
Posts: 4330
Joined: 29 Mar 2015, 09:41
Contact:

Re: RegExReplace - add many options

19 Feb 2018, 17:12

Albireo wrote:Is there anyone who can help me to translate?
Take a look here. Move the cursor over the line with the RegEx.
Albireo
Posts: 1756
Joined: 16 Oct 2013, 13:53

Re: RegExReplace - add many options

19 Feb 2018, 17:13

teadrinker!
If the vText is changed to this .:

Code: Select all

vText := " `t This     `nis, ""only"" a  small  `ntest.     "
MsgBox 64, Row.: %A_LineNumber% -> %A_ScriptName%, % "|" RegExReplace(vText, "(^""\s+|\s+""$)|(?<=\S\s)\s+") "|"
It doesn't work!
teadrinker
Posts: 4330
Joined: 29 Mar 2015, 09:41
Contact:

Re: RegExReplace - add many options

19 Feb 2018, 17:17

Now it works again:

Code: Select all

vText := " `t This     `nis, ""only"" a  small  `ntest.     "
MsgBox 64, Row.: %A_LineNumber% -> %A_ScriptName%, % "|" RegExReplace(vText, "(^""?\s+|\s+""?$)|(?<=\S\s)\s+") "|"
 
:)
Albireo
Posts: 1756
Joined: 16 Oct 2013, 13:53

Re: RegExReplace - add many options

19 Feb 2018, 17:19

teadrinker wrote:
Albireo wrote:Is there anyone who can help me to translate?
Take a look here. Move the cursor over the line with the RegEx.
What exciting page for Regular Expressions...
Thank you!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Araphen, demon740, Google [Bot], mapcarter and 305 guests