Regex expression

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

kimberlyn
Posts: 18
Joined: Mon Jul 01, 2013 1:40 pm

Regex expression

Post by kimberlyn » Wed Jul 03, 2013 9:23 am

Hello everyone,

Today I have kind of an issue. I have a file containing ligns of values and expressions like :

Code: Select all

 ITEM1,10,3,50,ATK,DFS,SPE,[GOF,30,33,0]; 
 ITEM2,1,0,,ATK,DFS,SPE,[GDF,30,60,1];
you will notice that if there is no value, the field is empty.

and my code is like :

Code: Select all

 open file File for read
 
 repeat for each line currentLine in File
    if matchText(currenLine, "(......),...,...,...,...,...,...,[...,...,...];", vITEM) then
         answer vITEM
    end if
 end repeat 
I want (for now) to take the ITEM and put it into the variable vITEM
but it doesn't work. Maybe the way I do is wrong but I'm stuck at this :/

Thanks !

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: Regex expression

Post by shaosean » Wed Jul 03, 2013 11:38 am

If your data is pretty much always in that format, you could skip using regex and just use "item x of line y"

Code: Select all

open file File for read

repeat for each line currentLine in File
  answer (item 1 of currentLine)
end repeat

kimberlyn
Posts: 18
Joined: Mon Jul 01, 2013 1:40 pm

Re: Regex expression

Post by kimberlyn » Wed Jul 03, 2013 12:26 pm

Hi,

I've thought about that. But it is not that simple because the file sometimes is in the format (and I can't change it):

Code: Select all

ITEM1,10,3,50,ATK,DFS,SPE,[GOF,30,33,0]; 
ITEM2,1,0,,ATK,DFS,SPE,[GDF,30,60,1];
ITEM3,23,1,100,ATK,DFS,SPE,[
GUF,,99,12]; 
ITEM4,1,90,21,ATK,DFS,SPE,[
GDF,30,,12];
So if I only take the first element of each line, I'll get some wrong items. That's why I wanted to use regex expressions.

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Regex expression

Post by Klaus » Wed Jul 03, 2013 12:56 pm

Hi kimberlyn,
kimberlyn wrote:...So if I only take the first element of each line, I'll get some wrong items...
well, then do NOT take the first item from EACH line!

As a developer you are in control to do a simple CHECK:

Code: Select all

...
repeat for each line currentLine in tFileContent
  if item 1 of currentLine begins with "ITEM" then
    ## do something with that line that DOES begion with "ITEM"
  end if
end repeat
...
N'est-ce pas? 8-)


Best

Klaus

kimberlyn
Posts: 18
Joined: Mon Jul 01, 2013 1:40 pm

Re: Regex expression

Post by kimberlyn » Wed Jul 03, 2013 1:42 pm

Yes but it's kinf of "dirty".

However, I found my solution :

Code: Select all

repeat for each line currentLine in File
    if matchText(currenLine, "(.*?),.*?,.*?,.*?,.*?,.*?,.*?,\[.*?,?.*?,?.*?];$", vITEM) then
         answer vITEM
    end if
end repeat
This takes the first item of my line (with the "()") and store it into my variable ! Even if some parameters are empty :)

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Regex expression

Post by Klaus » Wed Jul 03, 2013 1:47 pm

kimberlyn wrote:Yes but it's kind of "dirty".
Not neccessarily, I just don't have any idea about RegEx! :-D

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: Regex expression

Post by shaosean » Wed Jul 03, 2013 3:38 pm

As mentioned in your other thread, the use of the lineDelimiter property would probably be easier and quicker than than overhead of regex...

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Regex expression

Post by Thierry » Wed Jul 03, 2013 5:09 pm

kimberlyn wrote: However, I found my solution :

Code: Select all

repeat for each line currentLine in File
    if matchText(currenLine, "(.*?),.*?,.*?,.*?,.*?,.*?,.*?,\[.*?,?.*?,?.*?];$", vITEM) then
         answer vITEM
    end if
end repeat
As you didnt' mentionned how much datas you have, well,
forget all about the next if you don't need it :)

To improve the speed of your regex,
you may replace each .*? with [^,]*?
This will avoid back-tracking.

You coud also use the repeat tag if you like; your regex will be:

Code: Select all

"^([^,]*?),([^,]*?,){6}\[([^,]*?,){3}[^,]*?;$"
And last, as you mentionned in another thread you need to get items 5 and 9,
you also could do it all in one:

Code: Select all

 if matchText(currentLine,"^([^,]*?),[^,]*?,[^,]*?,[^,]*?,([^,]*?),[^,]*?,[^,]*?,\[[^,]*?,([^,]*?),[^,]*?,[^,]*?;$",vITEM, v5, v9 ) then
                     put vITEM & v5 & v9
Regards,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: Regex expression

Post by bn » Wed Jul 03, 2013 6:33 pm

Salut Thierry,
"^([^,]*?),[^,]*?,[^,]*?,[^,]*?,([^,]*?),[^,]*?,[^,]*?,\[[^,]*?,([^,]*?),[^,]*?,[^,]*?;$"
could you stop that nonsense please. Je vais tomber dans les pommes... I always knew that the spam filter of the forum is bad, but I did not think it was that bad.
:) :)
Comment va la Belle-Hélène?

Kind regards
Bernd

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

Re: Regex expression

Post by mwieder » Wed Jul 03, 2013 6:46 pm

Thierry-

If you're typing that on your phone or tablet, there's a way to switch *off* the symbol keyboard and back to letters. :-)

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Regex expression

Post by Thierry » Wed Jul 03, 2013 7:11 pm

bn wrote:Salut Thierry,
could you stop that nonsense please.
Je vais tomber dans les pommes...
I always knew that the spam filter of the forum is bad, but I did not think it was that bad.
:) :)
Comment va la Belle-Hélène?

Kind regards
Bernd
Hallo Bernd,

Regexps are like cheese; some like it others not :)
Obviously, spam filters like it :)

By the way, it's more an accademic reply to a post about regexps;
never said I'll choose that way in this case :)

Belle-Hélène is coming soon.. Too much rain and cold weather these days plus
still slowly recovering from my last accident..

Regards,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Regex expression

Post by Thierry » Wed Jul 03, 2013 7:15 pm

mwieder wrote:Thierry-

If you're typing that on your phone or tablet, there's a way to switch *off* the symbol keyboard and back to letters. :-)
Hi Mark,

I'm too old to type some code on a phone :)

Otherwise, the copy-paste key is really powerful in this case :)

Regards,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

kimberlyn
Posts: 18
Joined: Mon Jul 01, 2013 1:40 pm

Re: Regex expression

Post by kimberlyn » Wed Jul 03, 2013 7:27 pm

Thierry wrote:

Code: Select all

"^([^,]*?),([^,]*?,){6}\[([^,]*?,){3}[^,]*?;$"
And last, as you mentionned in another thread you need to get items 5 and 9,
you also could do it all in one:

Code: Select all

 if matchText(currentLine,"^([^,]*?),[^,]*?,[^,]*?,[^,]*?,([^,]*?),[^,]*?,[^,]*?,\[[^,]*?,([^,]*?),[^,]*?,[^,]*?;$",vITEM, v5, v9 ) then
                     put vITEM & v5 & v9
Regards,

Thierry
Very interesting Thierry ! You seem to well know how regex works, so I would like you to help me improve it :)

I told my lines have the format

Code: Select all

ITEM1,10,3,50,ATK,DFS,SPE,[GOF,30,33,0];
but in fact they are more like

Code: Select all

ITEM1,10,3,50,ATK,DFS,SPE,[GOF,30,33,0;TTF,21,33,;JDK,10,20,45;...];
and I don't know how much parameters in the "[]" there can be. I only know that they are groups of 4 parameters like "XXX,nb1,nb2,nb3" separated by ";".

Is there a way to put that information in the regex, and if yes how can I access to these parameters ? (For example if I want to take the "JDK")

Thanks :)

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Regex expression

Post by Thierry » Wed Jul 03, 2013 8:09 pm

kimberlyn wrote: I told my lines have the format

Code: Select all

ITEM1,10,3,50,ATK,DFS,SPE,[GOF,30,33,0];
but in fact they are more like

Code: Select all

ITEM1,10,3,50,ATK,DFS,SPE,[GOF,30,33,0;TTF,21,33,;JDK,10,20,45;...];
and I don't know how much parameters in the "[]" there can be.
I only know that they are groups of 4 parameters like "XXX,nb1,nb2,nb3" separated by ";".

Is there a way to put that information in the regex, and if yes how can I access to these parameters ?
(For example if I want to take the "JDK")

Thanks :)
Well, if I understand correctly, you could do that:

Code: Select all

   if matchText(aLINE,";JDK,([0-9]*),([0-9]*),([0-9]*);",n1, n2, n3) then
      put  n1 & n2 & n3
   end if
 
HTH,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Regex expression

Post by Klaus » Wed Jul 03, 2013 8:10 pm

Ladies and Gentlemen,

attention please, this is the moderator speaking!
Thierry wrote:Regexps are like cheese..
You are not allowed to mention "cheese" with postings < 10000!
:-D :-D :-D

Post Reply