Regular Expression help with simple search and replace

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Regular Expression help with simple search and replace

Postby vernsworld » Thu Nov 05, 2009 12:51 am

Okay here's the story so far...

I have application files from a commercial animation program that are plain ascii text. The application in question has no backwards compatibility, you can't save to an older version, so I simply need to reformat a few "sections" of the file to convert to an older version of the file format.

I used Jedit to create the regex needed to do this in a few simple steps. Actually it only requires 3 regex "steps" to reformat. There are only a few areas that need changing. These areas are identical in structure but a file could have 100 or more instances that need to be reformatted. My thought is that it SHOULD be simple enough to put the regex into a RR stack for converting and saving these files.

I used the "Open Anything" sample stack to start my project. I can open the files and can do simple search and replace but some of the regex from my Jedit solution doesn't seem to work with "ReplaceText".

Is replaceText the right thing to use? I can't escape "quotes" or "tabs" for some reason. I am VERY new with RR but have experience with Lua and JS etc.

I found a "conference" stack discussing Regex but... I keep thinking my project should be so simple. All I need to do is open the file, search and remove single lines, search and replace small chunks of text. Very straight forward.

I could just use Jedit to do this but I think making a simple application to do this in "one click" would be useful. I thought it would be a good project to start with.

Any guidance appreciated.

-vern
vernsworld
 
Posts: 5
Joined: Tue Nov 03, 2009 4:30 am

Postby vernsworld » Thu Nov 05, 2009 2:51 am

I am going to simplify my question to some degree...

How the heck do you escape quotes in replaceText()?

-vern
vernsworld
 
Posts: 5
Joined: Tue Nov 03, 2009 4:30 am

Postby vernsworld » Thu Nov 05, 2009 4:21 am

Oh my... I think I figured this out... is this the only way?

here's sample text to find. There are leading tabs and quotes throughout the text:

Code: Select all
         "1" false true false true


To "find" the "1" I am using the following "regex" in RR:

Code: Select all
"(" & tab & "+" & quote & "[0-9]+" & quote & ")"


So the only way to do this for quotes and tabs is to make the regex even harder to read? This works... but wow... it's a bit klunky. Is it possible to "store" regex in a hidden field and read it in? Hey... that might work.

-vern
vernsworld
 
Posts: 5
Joined: Tue Nov 03, 2009 4:30 am

Postby vernsworld » Thu Nov 05, 2009 5:45 am

Woohooo! I'm golden. I can put text in hidden fields with all my regex code. I can use \011 to escape tabs since RR doesn't do \t. Because I'm using text in a field for the regex pattern I can escape quotes the way I'm use to \".

Now if this is all silly on my part let me know. If there is a better way to "store" regex with "escaped quotes" in code I'm all for it. I'm quite happy though.

;)

-vern
vernsworld
 
Posts: 5
Joined: Tue Nov 03, 2009 4:30 am

Postby sturgis » Thu Nov 05, 2009 5:48 am

Edit: Ok so you got the solution while I was typing. Grats!

Yep you can do it that way. Easiest method to get the regex setup is probably type it in in something else, copy and paste it into a field then use the field reference to run the regex.

I setup 2 fields and a button and attached this code to the button.

Code: Select all
on mouseUp
   get matchText(field "Field",field "field2",theText)
   put it && theText
end mouseUp


I pasted the regex into field 2, put the string to match in field one, and put the match in theText variable.

It contains true or false for a match found, theText contains the match text itself showing the regex worked, both displayed to the message box.

Its possible to setup the regex directly in rev, but it becomes a pain making you deal with quote this quote that stuff..

vernsworld wrote:Oh my... I think I figured this out... is this the only way?

here's sample text to find. There are leading tabs and quotes throughout the text:

Code: Select all
         "1" false true false true


To "find" the "1" I am using the following "regex" in RR:

Code: Select all
"(" & tab & "+" & quote & "[0-9]+" & quote & ")"


So the only way to do this for quotes and tabs is to make the regex even harder to read? This works... but wow... it's a bit klunky. Is it possible to "store" regex in a hidden field and read it in? Hey... that might work.

-vern
sturgis
Livecode Opensource Backer
Livecode Opensource Backer
 
Posts: 1562
Joined: Sat Feb 28, 2009 10:49 pm

Postby sturgis » Thu Nov 05, 2009 5:54 am

Hey thats cool! Thx for sharing. Got a question about \t though if someone wouldn't mind answering.

I put (\t+"[0-9]+") into my field to do the match and it worked like a champ, I assume that this is because.. while rev doesn't recognize /t the regex engine rev uses does?

vernsworld wrote:Woohooo! I'm golden. I can put text in hidden fields with all my regex code. I can use \011 to escape tabs since RR doesn't do \t. Because I'm using text in a field for the regex pattern I can escape quotes the way I'm use to ".

Now if this is all silly on my part let me know. If there is a better way to "store" regex with "escaped quotes" in code I'm all for it. I'm quite happy though.

;)

-vern
sturgis
Livecode Opensource Backer
Livecode Opensource Backer
 
Posts: 1562
Joined: Sat Feb 28, 2009 10:49 pm

Postby vernsworld » Thu Nov 05, 2009 6:24 pm

That's odd. I couldn't get \t to work at all. It could be that the pattern didn't work for some other reason and I changed the tab pattern and did other things to it.

I would love to get this as close to what I'm use to. I love to fiddle with regex and come up with some wicked complex patterns.

this is the pattern I need to fix for my project. It takes several passes to clear things out. I have to find this chunk first (as you see it is number "42" and there are usually many many more. 100's.) and remove the 4 "bracketed" keys.

Then another search finds the groups of "keys" below that section and removes the last column of numbers (0's in this case but could be something else).

I think with RR I can do this fairly easily once I get the hang of it. I plan to put my regex in ONE FIELD on lines. Saves having a bunch of fields. I can put a search/replace text on two lines in one field.

change this:

Code: Select all
         "42" false true false true
         [
            keys 1
               0 1 0.1 0.5 1
         ]
         [
            keys 1
               0 1 0.1 0.5 0
         ]
         [
            keys 1
               0 1 0.1 0.5 0 0
         ]
            4
               42 0 0
               42 1 0
               42 2 0
               42 3 0


To this:
Code: Select all
         "42" false true false true
            4
               42 0
               42 1
               42 2
               42 3


-vern
vernsworld
 
Posts: 5
Joined: Tue Nov 03, 2009 4:30 am

Postby sturgis » Thu Nov 05, 2009 6:50 pm

Yep, single field idea is a good one. In fact you could use a table field and put your match string tab replace string in, then reference each by item and line

get item 1 of line 1
get item 2 of line 1

Tons of ways you could do it. Properties, arrays, whatever.
sturgis
Livecode Opensource Backer
Livecode Opensource Backer
 
Posts: 1562
Joined: Sat Feb 28, 2009 10:49 pm


Return to Talking LiveCode

Who is online

Users browsing this forum: No registered users and 1 guest