Replace two RegEx lines with one, how? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Replace two RegEx lines with one, how?

13 Jul 2022, 12:32

Hello.

I use the lines below borrowed from forums to reomve % sign in the end of MYvar and the first line to remove Space (First character). This works but how to make just one line of RegEx instead of two?

Code: Select all

Myvar := Clipboard
Myvar := " 50%"    ; For example
Myvar := RegExReplace(Myvar, "\s")
Myvar := RegExReplace(Myvar, "%$" )
MsgBox, No space=%Myvar%=No sign
Thank you meanwhile
Last edited by Krd on 13 Jul 2022, 13:01, edited 3 times in total.
User avatar
boiler
Posts: 17206
Joined: 21 Dec 2014, 02:44

Re: Replace two RegEx lines with one, how?

13 Jul 2022, 12:45

Code: Select all

Myvar := RegExReplace(Myvar, "^\s(.*)%$", "$1")
Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: Replace two RegEx lines with one, how?

13 Jul 2022, 13:02

Weird :S

It workds with my example but not in action like the two lines above. Hmmm. How to check where it fails?
User avatar
boiler
Posts: 17206
Joined: 21 Dec 2014, 02:44

Re: Replace two RegEx lines with one, how?

13 Jul 2022, 13:07

Does your copied text contain line breaks in it? If so, add the s option so the dot matches all newline characters:

Code: Select all

Myvar := RegExReplace(Myvar, "s)^\s(.*)%$", "$1")
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Replace two RegEx lines with one, how?  Topic is solved

13 Jul 2022, 14:05

Code: Select all

Myvar := RegExReplace(Myvar, "(\s|%$)")
Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: Replace two RegEx lines with one, how?

14 Jul 2022, 04:20

Many thanks to you boiler and AlphaBravo.

Alphas verion worked.

Boiler, my bad, yes it seems to be true that it has a linebreak. If I copy and paste the values in between two qutation marks, it would be:

Code: Select all

Myvar :="60%
"
Weird, this time the space/linebreak is at end...
Can you make changes to your code? Curious where the change is now :)
User avatar
boiler
Posts: 17206
Joined: 21 Dec 2014, 02:44

Re: Replace two RegEx lines with one, how?

14 Jul 2022, 04:51

I meant linebreaks between, not on the end. My code currently expects the space and percent sign to be the first and last characters of the string. And I showed how to handle linebreaks between.

Instead of stripping specific characters, if your goal is really to extract the number, then it can just be this:

Code: Select all

 MyVar := "
(
 50%

)"
RegExMatch(MyVar, "\d+", MyVar)
MsgBox, % MyVar
Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: Replace two RegEx lines with one, how?

14 Jul 2022, 08:20

Yes, that whay im after and didn't even think the way you do. :facepalm:

But what to add to catch the decimental number like 4,2? As it just catches the first left number now.

Is this the right way to catch demintal numbers?

Code: Select all

RegExMatch(MyVar, "\d+,\d+", MyVar)
User avatar
boiler
Posts: 17206
Joined: 21 Dec 2014, 02:44

Re: Replace two RegEx lines with one, how?

14 Jul 2022, 10:26

That would only work if the number has a decimal point (comma) and would not catch it otherwise. You can capture it like this:

Code: Select all

RegExMatch(MyVar, "[\d,]+", MyVar)
Or like this if you want it to enforce that the decimal separator be in the right place and only one of them, if it exists:

Code: Select all

RegExMatch(MyVar, "\d+(,\d*)?", MyVar)
Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: Replace two RegEx lines with one, how?

14 Jul 2022, 11:26

@boiler

Perfecto!
Many thanks master! Now I see this

Code: Select all

(,\d*)?
and I like it :bravo: For a moment I was about to ask Bobo to explain to the noob what this does but I learned from the side you linked earlier :D

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: kaka2, Rohwedder and 110 guests