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',
How to add special character quotes to such characters Topic is solved
Re: How to add special character quotes to such characters
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
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
That is not what your example shows.
Re: How to add special character quotes to such characters
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
sofista wrote: ↑31 Jul 2021, 09:09Hi: 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
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
Thank you very much. Can you explain the usage of 'am', '$1': '$2'sofista wrote: ↑01 Aug 2021, 22:03Sure, 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
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.
$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
thank you very muchsofista wrote: ↑02 Aug 2021, 11:25Glad 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
You're welcome. At first, it may be difficult to learn regex, but it is worth it