Page 1 of 1

Problem with regex at start of line

Posted: Wed Feb 20, 2019 8:56 am
by kaveh1000
I am reasonably familiar with Regex and its implementation in LiveCode. I currently have a problem that relates to the start of string, i.e. using the caret: "^".

In the attached stack I am trying to remove all leading spaces from each line, using:

Code: Select all

put replacetext( \
         fld "Original",  \
         "(^ +)",  \
         "")  \
         into fld "Replacement"
The caret should tell the code to remove spaces only at the start of lines and nowhere else. But it does not match any string. Removing the caret of course removes all spaces.

I could use more complex constructions, e.g. "char before is a return", but I want to keep it simple.

Grateful for any ideas.

Regards
Kaveh

Re: Problem with regex at start of line

Posted: Wed Feb 20, 2019 9:57 am
by Thierry
Hi Kaveh,

Your regex will suppress *only* the first spaces of the beginning of the whole text !

Try this one instead:
"(?m)^(\s+)"
will do it on every line.

The clue hereis (?m) -> mode multi-line which changes the meaning of the caret.

Regards,

Thierry

Re: Problem with regex at start of line

Posted: Wed Feb 20, 2019 10:03 am
by kaveh1000
Thierry to the rescue! That works. I had never come across that!

Thank you as ever...

Regards
Kaveh

Re: Problem with regex at start of line

Posted: Wed Feb 20, 2019 1:01 pm
by Klaus
I had no idea that one can use \ inside of the params of a function!? :shock:

Re: Problem with regex at start of line

Posted: Wed Feb 20, 2019 3:06 pm
by bogs
Me either, that is awesome!

Thank you Thierry!

Re: Problem with regex at start of line

Posted: Wed Feb 20, 2019 3:12 pm
by dunbarx
Klaus.

Neither did I. This works:

Code: Select all

on mouseUp
   answer doubleValue(25 +\
   25)
end mouseUp

function doubleValue var
   return var * 2
end doubleValue
But this does not:

Code: Select all

on mouseUp
   answer doubleValue(2\
   5 + 25)
end mouseUp
Craig

Re: Problem with regex at start of line

Posted: Wed Feb 20, 2019 4:29 pm
by kaveh1000
I normally use the backslash breaks with multi-parameter functions. The indenting makes things more readable, especially when there is regex as a parameter!!