Page 1 of 2

What is return character in regex?

Posted: Fri Jan 23, 2015 12:00 am
by kaveh1000
I have three fields, and using the following script:

put the replacetext (field "text", field "find", field "replace") into field "text"

Not able to match a return character in field "text". I have tried the obvious \r. Help please?

Re: What is return character in regex?

Posted: Fri Jan 23, 2015 9:03 pm
by mwieder
Kaveh-

regex by default doesn't deal with multiple lines. You need to give it the multi-line option (?s)

Code: Select all

put replacetext (field "text", "(?s) & "field "find" & "\s", field "replace") into field "text"
will do the trick.

Re: What is return character in regex?

Posted: Fri Jan 23, 2015 10:06 pm
by kaveh1000
Great. Thank you Mark. Can you give me a pointer to where I can find full documentation for livecode Regex?

Re: What is return character in regex?

Posted: Fri Jan 23, 2015 11:03 pm
by mwieder
LiveCode uses the standard pcre library, so any regex documentation should work.
But regex structures can get pretty complicated quickly.
Your best bet would probably be to try one of the many online regex testers and fiddle with things until it works.
Or try querying stackoverflow for particular questions.

Re: What is return character in regex?

Posted: Sat Jan 24, 2015 11:38 am
by kaveh1000
OK Mark. Good to know it is standard regex. But I thought the code you gave me was not standard. I will try a few things, then bother you again.

I am reasonably familiar with regex so hopefully that is not a problem. I just want to use what I did before in text editor, but in LiveCode with the obvious advantages.

By the way last time I looked I think there was no regex "look-ahead" implemented. I will try again.

Re: What is return character in regex?

Posted: Sat Jan 24, 2015 11:38 am
by kaveh1000
OK Mark. Good to know it is standard regex. But I thought the code you gave me was not standard. I will try a few things, then bother you again.

I am reasonably familiar with regex so hopefully that is not a problem. I just want to use what I did before in text editor, but in LiveCode with the obvious advantages.

By the way last time I looked I think there was no regex "look-ahead" implemented. I will try again.

Re: What is return character in regex?

Posted: Sat Jan 24, 2015 1:46 pm
by kaveh1000
OK. I must be missing something really obvious. I have a field "text" with

Code: Select all

one
two

three
I want to replace multiple returns with a single return. So I use:

Code: Select all

put replacetext(fld "text", "\v+", "\r")
but I get a result of

Code: Select all

one\rtwo\rthree
How can I insert a return character in the replacement text? I have tried \s etc. Is there a modifier to allow special chars to be inserted and if so where do I put that modifier?

Re: What is return character in regex?

Posted: Sat Jan 24, 2015 5:46 pm
by jacque
The only parameter that uses regex is the second one that supplies the actual regular expression. The first and third use normal LiveCode syntax. The LiveCode constant for a return character is the word "return" or "cr". Use one of those as the last parameter, without quotes since they are constants.

Re: What is return character in regex?

Posted: Sat Jan 24, 2015 9:50 pm
by mwieder
Kaveh-

What Jacque said. What you're trying to do now is different from your original post, and regex won't apply for the replacement text.

Re: What is return character in regex?

Posted: Sat Jan 24, 2015 10:08 pm
by SparkOut
Also try

Code: Select all

filter field "text" without empty

Re: What is return character in regex?

Posted: Sun Jan 25, 2015 12:31 am
by mwieder
Hah! I was just about to post the same thing. Although in long form I'd actually use

Code: Select all

lock screen
put field "test" into tVar
filter tVar without empty
put tVar into field "test"
unlock screen
So, Kaveh, maybe you should be more explicit about what you're trying to do here... if you just want to condense all multiple returns down to one then I'd use the filter approach. But if you just want to remove specific repeats then you'll probably want something more complex.

Re: What is return character in regex?

Posted: Sun Jan 25, 2015 1:05 am
by jacque
SparkOut wrote:Also try

Code: Select all

filter field "text" without empty
This is my favoritest thing.

Re: What is return character in regex?

Posted: Sun Jan 25, 2015 4:47 am
by mwieder
Kinda like "focus on nothing", eh?

Re: What is return character in regex?

Posted: Sun Jan 25, 2015 7:02 am
by jacque
Yeah. I'm easily amused.

Re: What is return character in regex?

Posted: Sun Jan 25, 2015 7:26 am
by mwieder