How to add special character quotes to such characters Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
wtu123
Posts: 19
Joined: 15 Nov 2020, 22:54

How to add special character quotes to such characters

31 Jul 2021, 05:22

qdxq: nu
qdmode: 2
todaysay:
fastreply: 0

Here is a header file. I want to add quotation marks to convert it into the following form. How should I write the program?
Thank you teachers.

'qdxq': 'nu',
'qdmode': '2',
'todaysay: '',
'fastreply: 0',
User avatar
mikeyww
Posts: 26438
Joined: 09 Sep 2014, 18:38

Re: How to add special character quotes to such characters

31 Jul 2021, 07:53

Since you are showing three different formats, how would you describe the changes that you would like to see in the strings?
wtu123
Posts: 19
Joined: 15 Nov 2020, 22:54

Re: How to add special character quotes to such characters

31 Jul 2021, 08:37

mikeyww wrote:
31 Jul 2021, 07:53
Since you are showing three different formats, how would you describe the changes that you would like to see in the strings?
I guess it might need a loop.

When I select the characters to be converted, the program first copies all the characters, then looks for the newline character, cuts it into several segments, and then add ‘’ 'to the front and rear characters with': 'as the dividing point. Finally, all the modified contents are combined and output.
User avatar
mikeyww
Posts: 26438
Joined: 09 Sep 2014, 18:38

Re: How to add special character quotes to such characters

31 Jul 2021, 09:07

That is not what your example shows.
sofista
Posts: 644
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: How to add special character quotes to such characters

31 Jul 2021, 09:09

Hi: Here is an example code that does what you want. However, it would be better to provide further criteria, as mikeyww suggested, because input data may change in ways that are impossible for us to anticipate.

Code: Select all

v := "
(
qdxq: nu
qdmode: 2
todaysay:
fastreply: 0
)"

v := RegExReplace(v, "todaysay:", "'$0 '',")
v := RegExReplace(v, "fastreply: \d", "'$0',")
MsgBox, % v := RegExReplace(v,"(q.+?): (.+?)\R", "'$1': '$2',`n")
return

/* Output:

'qdxq': 'nu',
'qdmode': '2',
'todaysay: '',
'fastreply: 0',
 */
wtu123
Posts: 19
Joined: 15 Nov 2020, 22:54

Re: How to add special character quotes to such characters

31 Jul 2021, 23:24

mikeyww wrote:
31 Jul 2021, 09:07
That is not what your example shows.

Sorry, I made a mistake. The correct effect should be like this.


'qdxq':'nu',
'qdmode':'2',
'todaysay':'',
'fastreply':'0',
wtu123
Posts: 19
Joined: 15 Nov 2020, 22:54

Re: How to add special character quotes to such characters

31 Jul 2021, 23:27

sofista wrote:
31 Jul 2021, 09:09
Hi: Here is an example code that does what you want. However, it would be better to provide further criteria, as mikeyww suggested, because input data may change in ways that are impossible for us to anticipate.

Code: Select all

v := "
(
qdxq: nu
qdmode: 2
todaysay:
fastreply: 0
)"

v := RegExReplace(v, "todaysay:", "'$0 '',")
v := RegExReplace(v, "fastreply: \d", "'$0',")
MsgBox, % v := RegExReplace(v,"(q.+?): (.+?)\R", "'$1': '$2',`n")
return

/* Output:

'qdxq': 'nu',
'qdmode': '2',
'todaysay: '',
'fastreply: 0',
 */


Thank you, but there is a mistake in my example. Could you please help me modify it again?
sofista
Posts: 644
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: How to add special character quotes to such characters  Topic is solved

01 Aug 2021, 22:03

Sure, no problem. Try this code:

Code: Select all

SampleText := "
(
qdxq: nu
qdmode: 2
todaysay:
fastreply: 0
)"

MsgBox, % SampleText := RegExReplace(SampleText,"`am)(.+?): ?(.*)?", "'$1':'$2',")
return

/* Output:
'qdxq':'nu',
'qdmode':'2',
'todaysay':'',
'fastreply':'0',
 */
wtu123
Posts: 19
Joined: 15 Nov 2020, 22:54

Re: How to add special character quotes to such characters

01 Aug 2021, 23:02

sofista wrote:
01 Aug 2021, 22:03
Sure, no problem. Try this code:

Code: Select all

SampleText := "
(
qdxq: nu
qdmode: 2
todaysay:
fastreply: 0
)"

MsgBox, % SampleText := RegExReplace(SampleText,"`am)(.+?): ?(.*)?", "'$1':'$2',")
return

/* Output:
'qdxq':'nu',
'qdmode':'2',
'todaysay':'',
'fastreply':'0',
 */
Thank you very much. Can you explain the usage of 'am', '$1': '$2'
sofista
Posts: 644
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: How to add special character quotes to such characters

02 Aug 2021, 11:25

Glad to be of help. `a and m are options for regex patterns. m, for multi-line, views input data as a collection of lines. `a recognizes any type of newline. Taken together, they allow line-by-line processing of incoming text in a reliable way.

$1, $2 ... $n, where n means any number, are used in a replacement string to make a reference to matched substrings placed inside a set of parentheses, like (.+?) or (.*) posted above. Since captured groups are numbered from left to right, $1 is the first captured group, $2 is the second captured group and so on.

Details in RegExReplace and related pages.
wtu123
Posts: 19
Joined: 15 Nov 2020, 22:54

Re: How to add special character quotes to such characters

03 Aug 2021, 01:28

sofista wrote:
02 Aug 2021, 11:25
Glad to be of help. `a and m are options for regex patterns. m, for multi-line, views input data as a collection of lines. `a recognizes any type of newline. Taken together, they allow line-by-line processing of incoming text in a reliable way.

$1, $2 ... $n, where n means any number, are used in a replacement string to make a reference to matched substrings placed inside a set of parentheses, like (.+?) or (.*) posted above. Since captured groups are numbered from left to right, $1 is the first captured group, $2 is the second captured group and so on.

Details in RegExReplace and related pages.
thank you very much
sofista
Posts: 644
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: How to add special character quotes to such characters

03 Aug 2021, 08:56

You're welcome. At first, it may be difficult to learn regex, but it is worth it ;)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: gongnl, Google [Bot], inseption86, jaquesy, Mannaia666, Pareidol, RussF and 150 guests