Page 1 of 1
Replace two RegEx lines with one, how?
Posted: 13 Jul 2022, 12:32
by Krd
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
Re: Replace two RegEx lines with one, how?
Posted: 13 Jul 2022, 12:45
by boiler
Code: Select all
Myvar := RegExReplace(Myvar, "^\s(.*)%$", "$1")
Re: Replace two RegEx lines with one, how?
Posted: 13 Jul 2022, 13:02
by Krd
Weird :S
It workds with my example but not in action like the two lines above. Hmmm. How to check where it fails?
Re: Replace two RegEx lines with one, how?
Posted: 13 Jul 2022, 13:07
by boiler
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")
Re: Replace two RegEx lines with one, how? Topic is solved
Posted: 13 Jul 2022, 14:05
by AlphaBravo
Code: Select all
Myvar := RegExReplace(Myvar, "(\s|%$)")
Re: Replace two RegEx lines with one, how?
Posted: 14 Jul 2022, 04:20
by Krd
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:
Weird, this time the space/linebreak is at end...
Can you make changes to your code? Curious where the change is now
Re: Replace two RegEx lines with one, how?
Posted: 14 Jul 2022, 04:51
by boiler
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
Re: Replace two RegEx lines with one, how?
Posted: 14 Jul 2022, 08:20
by Krd
Yes, that whay im after and didn't even think the way you do.
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)
Re: Replace two RegEx lines with one, how?
Posted: 14 Jul 2022, 10:26
by boiler
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)
Re: Replace two RegEx lines with one, how?
Posted: 14 Jul 2022, 11:26
by Krd
@boiler
Perfecto!
Many thanks master! Now I see this
and I like it
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