Problem with regex at start of line

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

Post Reply
kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 329
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Problem with regex at start of line

Post by kaveh1000 » Wed Feb 20, 2019 8:56 am

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
Attachments
grep.livecode.zip
(1.59 KiB) Downloaded 13 times
Kaveh

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 648
Joined: Wed Nov 22, 2006 3:42 pm
Location: France
Contact:

Re: Problem with regex at start of line

Post by Thierry » Wed Feb 20, 2019 9:57 am

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
Thierry Douez - https://sunny-tdz.com
Pourquoi tant de notes lorsqu'il suffit de jouer les plus belles... [Barbara]

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 329
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Re: Problem with regex at start of line

Post by kaveh1000 » Wed Feb 20, 2019 10:03 am

Thierry to the rescue! That works. I had never come across that!

Thank you as ever...

Regards
Kaveh
Kaveh

Klaus
Posts: 11207
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Problem with regex at start of line

Post by Klaus » Wed Feb 20, 2019 1:01 pm

I had no idea that one can use \ inside of the params of a function!? :shock:

bogs
Posts: 3804
Joined: Sat Feb 25, 2017 10:45 pm

Re: Problem with regex at start of line

Post by bogs » Wed Feb 20, 2019 3:06 pm

Me either, that is awesome!

Thank you Thierry!
Image

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6175
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Problem with regex at start of line

Post by dunbarx » Wed Feb 20, 2019 3:12 pm

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

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 329
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Re: Problem with regex at start of line

Post by kaveh1000 » Wed Feb 20, 2019 4:29 pm

I normally use the backslash breaks with multi-parameter functions. The indenting makes things more readable, especially when there is regex as a parameter!!
Kaveh

Post Reply

Return to “Talking LiveCode”