Problem with regex at start of line

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

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

Problem with regex at start of line

Post 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
Attachments
grep.livecode.zip
(1.59 KiB) Downloaded 429 times
Kaveh
Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Problem with regex at start of line

Post 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
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 539
Joined: Sun Dec 18, 2011 7:23 pm
Contact:

Re: Problem with regex at start of line

Post by kaveh1000 »

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

Thank you as ever...

Regards
Kaveh
Kaveh
Klaus
Posts: 14324
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Problem with regex at start of line

Post by Klaus »

I had no idea that one can use \ inside of the params of a function!? :shock:
bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Problem with regex at start of line

Post by bogs »

Me either, that is awesome!

Thank you Thierry!
Image
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10502
Joined: Wed May 06, 2009 2:28 pm

Re: Problem with regex at start of line

Post 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
kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 539
Joined: Sun Dec 18, 2011 7:23 pm
Contact:

Re: Problem with regex at start of line

Post 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!!
Kaveh
Post Reply