Page 1 of 2
Regex expression
Posted: Wed Jul 03, 2013 9:23 am
by kimberlyn
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 !
Re: Regex expression
Posted: Wed Jul 03, 2013 11:38 am
by shaosean
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
Re: Regex expression
Posted: Wed Jul 03, 2013 12:26 pm
by kimberlyn
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.
Re: Regex expression
Posted: Wed Jul 03, 2013 12:56 pm
by Klaus
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?
Best
Klaus
Re: Regex expression
Posted: Wed Jul 03, 2013 1:42 pm
by kimberlyn
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

Re: Regex expression
Posted: Wed Jul 03, 2013 1:47 pm
by Klaus
kimberlyn wrote:Yes but it's kind of "dirty".
Not neccessarily, I just don't have any idea about RegEx!

Re: Regex expression
Posted: Wed Jul 03, 2013 3:38 pm
by shaosean
As mentioned in your other thread, the use of the lineDelimiter property would probably be easier and quicker than than overhead of regex...
Re: Regex expression
Posted: Wed Jul 03, 2013 5:09 pm
by Thierry
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
Re: Regex expression
Posted: Wed Jul 03, 2013 6:33 pm
by bn
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
Re: Regex expression
Posted: Wed Jul 03, 2013 6:46 pm
by mwieder
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.

Re: Regex expression
Posted: Wed Jul 03, 2013 7:11 pm
by Thierry
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
Re: Regex expression
Posted: Wed Jul 03, 2013 7:15 pm
by Thierry
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
Re: Regex expression
Posted: Wed Jul 03, 2013 7:27 pm
by kimberlyn
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

Re: Regex expression
Posted: Wed Jul 03, 2013 8:09 pm
by Thierry
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
Re: Regex expression
Posted: Wed Jul 03, 2013 8:10 pm
by Klaus
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!
