Page 1 of 1

How to add special character quotes to such characters

Posted: 31 Jul 2021, 05:22
by wtu123
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',

Re: How to add special character quotes to such characters

Posted: 31 Jul 2021, 07:53
by mikeyww
Since you are showing three different formats, how would you describe the changes that you would like to see in the strings?

Re: How to add special character quotes to such characters

Posted: 31 Jul 2021, 08:37
by wtu123
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.

Re: How to add special character quotes to such characters

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

Re: How to add special character quotes to such characters

Posted: 31 Jul 2021, 09:09
by sofista
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',
 */

Re: How to add special character quotes to such characters

Posted: 31 Jul 2021, 23:24
by wtu123
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',

Re: How to add special character quotes to such characters

Posted: 31 Jul 2021, 23:27
by wtu123
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?

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

Posted: 01 Aug 2021, 22:03
by sofista
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',
 */

Re: How to add special character quotes to such characters

Posted: 01 Aug 2021, 23:02
by wtu123
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'

Re: How to add special character quotes to such characters

Posted: 02 Aug 2021, 11:25
by sofista
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.

Re: How to add special character quotes to such characters

Posted: 03 Aug 2021, 01:28
by wtu123
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

Re: How to add special character quotes to such characters

Posted: 03 Aug 2021, 08:56
by sofista
You're welcome. At first, it may be difficult to learn regex, but it is worth it ;)