Page 2 of 2

Re: What is return character in regex?

Posted: Sun Jan 25, 2015 12:11 pm
by kaveh1000
Thanks all for the pointers, and I agree with Mark that I failed to define the problem clearly! So here it is. Please look at the attached screenshot. The button "Clean text" has the following script:

Code: Select all

   put fld "text" into tTheText
   repeat with i = 1 to the number of lines of field "find"
      put  replacetext (tTheText, line i of fld "find", line i of fld "replace") into tTheText
   end repeat
   put tTheText into field "final text"
   
So it is a kind of user friendly Perl script. Ideally I want to put standard PCRE text in the replace box, but I am happy to put LiveCode constants like "return" if no other way. (Actually, putting return in "replace" is not working with or without quotes. I am sure that is a newbie question!)

This is not the primary goal, but it would be nice to click a button when the script is working, and generate a Perl or Python script.

Thanks for the help so far and any further ideas appreciated.

Re: What is return character in regex?

Posted: Sun Jan 25, 2015 8:21 pm
by mwieder
Kaveh-

Try this:

Code: Select all

on mouseUp
  local tText
  local tReplace
  
  lock screen
  put fld "fldInitialText" into tText
  repeat with i = 1 to the number of lines of field "fldFind"
    put line i of field "fldReplace" into tReplace
    replace "\r" with return in tReplace
    put  replacetext (tText, line i of fld "fldFind", tReplace) into tText
  end repeat
  put tText into field "fldFinalText"
  unlock screen
end mouseUp
with field "fldFind":
(?s)\\item[\s]+
(?s)[\n]+\\begin{itemize}

and field "fldReplace":
\item
\r\begin{itemize}

Note: you seemed to want to use "\r" in the replacement string, but note that you could just as easily use "\n" with the appropriate change to the code.

Re: What is return character in regex?

Posted: Sun Jan 25, 2015 8:48 pm
by kaveh1000
Wow. Thanks for spending the time in this Mark. :-) Next drink is on you. ;-)

I am going to look at this tomorrow, but quick question: can I put the (?s) in the script to keep the find lines clean? In fact, are you sure that is needed? I think for me the multiline search worked without the (?s)

Re: What is return character in regex?

Posted: Sun Jan 25, 2015 9:45 pm
by mwieder
I'll leave the experimenting to you.
Shouldn't be any problem with moving the modifier string into the code. Nice catch - I didn't think of that.
I didn't try it without the modifiers - regex *normally* works with a single string, so a newline char ends the match attempt, but if it works it works.