What is return character in regex?

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

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

What is return character in regex?

Post by kaveh1000 » Fri Jan 23, 2015 12:00 am

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?
Kaveh

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: What is return character in regex?

Post by mwieder » Fri Jan 23, 2015 9:03 pm

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.

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

Re: What is return character in regex?

Post by kaveh1000 » Fri Jan 23, 2015 10:06 pm

Great. Thank you Mark. Can you give me a pointer to where I can find full documentation for livecode Regex?
Kaveh

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: What is return character in regex?

Post by mwieder » Fri Jan 23, 2015 11:03 pm

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.

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

Re: What is return character in regex?

Post by kaveh1000 » Sat Jan 24, 2015 11:38 am

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.
Kaveh

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

Re: What is return character in regex?

Post by kaveh1000 » Sat Jan 24, 2015 11:38 am

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.
Kaveh

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

Re: What is return character in regex?

Post by kaveh1000 » Sat Jan 24, 2015 1:46 pm

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?
Kaveh

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: What is return character in regex?

Post by jacque » Sat Jan 24, 2015 5:46 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: What is return character in regex?

Post by mwieder » Sat Jan 24, 2015 9:50 pm

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.

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: What is return character in regex?

Post by SparkOut » Sat Jan 24, 2015 10:08 pm

Also try

Code: Select all

filter field "text" without empty

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: What is return character in regex?

Post by mwieder » Sun Jan 25, 2015 12:31 am

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.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: What is return character in regex?

Post by jacque » Sun Jan 25, 2015 1:05 am

SparkOut wrote:Also try

Code: Select all

filter field "text" without empty
This is my favoritest thing.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: What is return character in regex?

Post by mwieder » Sun Jan 25, 2015 4:47 am

Kinda like "focus on nothing", eh?

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: What is return character in regex?

Post by jacque » Sun Jan 25, 2015 7:02 am

Yeah. I'm easily amused.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: What is return character in regex?

Post by mwieder » Sun Jan 25, 2015 7:26 am


Post Reply