[SOLVED] REGEX Searching

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

Post Reply
stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

[SOLVED] REGEX Searching

Post by stam » Mon Jul 27, 2020 12:22 am

hi all,

I'm trying to convert projects from other environments and use regex for a fair number of projects to parse for text items between 2 patterns.
However i can't seem to get this working in LC

Example text to search (a tiny fragment of a huge text file - in this example i want to extract the 'Global' values):

Code: Select all

Timing Information
==========================================================
Clip Duration;960.531;ms
Average RR Duration;960.531;ms
Enddiastolic frame time;8599.53;ms
Endsystolic  frame time;7996.41;ms
Reference time;7661.34;ms
Trigger time;7661.34;ms



Results
==========================================================
Global
----------------------------------------------------------
EDV;124.35;ml
EDVi;--.--;
ESV;51.52;ml
ESVi;--.--;
SV;72.83;ml
EF;58.57;%
Mass;101.43;g
SDI;5.47;%
GLS;-19.01;%
GCS;-28.13;%
Twist;8.87;°
Torsion;0.94;°/cm
ED;8599.53;ms
ES;7994.17;ms
SDII;2.20;%



Results
==========================================================
Strain
Principal tangential
T2P
Minimum
----------------------------------------------------------
Ase15Model16SegmentZoning Segment  0 [ average];346.24;ms
----------------------------------------------------------
Ase15Model16SegmentZoning Segment  1 [     ant];330.60;ms
Ase15Model16SegmentZoning Segment  2 [ant/sept];337.30;ms
Ase15Model16SegmentZoning Segment  3 [inf/sept];509.31;ms
Ase15Model16SegmentZoning Segment  4 [     inf];402.08;ms
Ase15Model16SegmentZoning Segment  5 [ inf/lat];368.58;ms
Ase15Model16SegmentZoning Segment  6 [ ant/lat];359.64;ms
----------------------------------------------------------
Ase15Model16SegmentZoning Segment  7 [     ant];337.30;ms
Ase15Model16SegmentZoning Segment  8 [ant/sept];397.62;ms
Ase15Model16SegmentZoning Segment  9 [inf/sept];489.20;ms
Ase15Model16SegmentZoning Segment 10 [     inf];502.60;ms
Ase15Model16SegmentZoning Segment 11 [ inf/lat];352.94;ms
Ase15Model16SegmentZoning Segment 12 [ ant/lat];348.47;ms
----------------------------------------------------------
Ase15Model16SegmentZoning Segment 13 [     ant];357.41;ms
Ase15Model16SegmentZoning Segment 14 [    sept];366.34;ms
Ase15Model16SegmentZoning Segment 15 [     inf];332.84;ms
Ase15Model16SegmentZoning Segment 16 [     lat];348.47;ms
----------------------------------------------------------


The regex below works in all other environments i use:

Code: Select all

(?msi)(?<=Global\n----------------------------------------------------------\n).*?(?=\n\n\nResult)
in all other environments (eg BBEdit, RegExRX) this yields:

Code: Select all

EDV;124.35;ml
EDVi;--.--;
ESV;51.52;ml
ESVi;--.--;
SV;72.83;ml
EF;58.57;%
Mass;101.43;g
SDI;5.47;%
GLS;-19.01;%
GCS;-28.13;%
Twist;8.87;°
Torsion;0.94;°/cm
ED;8599.53;ms
ES;7994.17;ms
SDII;2.20;%
I guess i'm not doing it right in LC - the code i use is:

Code: Select all

Local tQuery
put "(?msi)(?<=Global\n----------------------------------------------------------\n).*?(?=\n\n\nResult)" into tQuery
filter field "the field containing all text" with regex pattern tQuery into field "the field to showing the result"
But the result is empty...

Grateful for help with this...

Edit: I realise i'm probably 'doing it wrong' -- have tried MatchText and MatchChunk with no luck. It's also possible that regex lookarounds don't work in this context, but i don't know enough about what works with LiveCode...

I have a wordy workaround using chunk expression which works, but inelegant and many more lines of code, so ideally would go with a regex solution if that were possible... grateful for any livecode/regex gurus to chime in!
Last edited by stam on Thu Jul 30, 2020 9:00 pm, edited 2 times in total.

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: REGEX Searching

Post by stam » Mon Jul 27, 2020 1:23 pm

Hi guys - i've concluded it's probably my lack of knowledge on how to use this regex with LC -- the regex certainly works and i use this regularly. From what I can see by the standards LC follows for this, lookarounds should work...

I attach a screen shot of BBEdit illustrating the desired outcome - just hope i can use this to select out data from these files in LC, as chuck expressions are proving to be a bit on the slower side...

Many thanks for your help with this LC Community!

Image

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

Re: REGEX Searching

Post by Thierry » Mon Jul 27, 2020 3:46 pm

Hi Stam,

Your regex looks good, but...

You can't simply use the filter in your case;
please read carefully the last line in the description section for filter in the Dictionary.

Here is a working solution with matchText().
My guess is you forgot to group the wanting part of the pattern in your regex for this to work...

In action with the output on the right side:

thierry 4 Stam.jpg

Kind regards,

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

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: REGEX Searching

Post by stam » Mon Jul 27, 2020 5:56 pm

Fantastic, thank you Thierry

You're right - was relying on lookarounds and not grouping. I had tried matchText but couldn't get it to work previously. However using your example of regex with the grouping worked perfectly, thank you!

more importantly... what IDE are you using??

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

Re: REGEX Searching

Post by Thierry » Tue Jul 28, 2020 8:07 am

stam wrote:
Mon Jul 27, 2020 5:56 pm
Fantastic, thank you Thierry
You're welcome.
I had tried matchText but couldn't get it to work previously. However using your example of regex with the grouping worked perfectly!
If you need to catch any part of a pattern with matchText() or matchChunk(), you need to use captured groups.
If you only need to check it a pattern is there, then you don't.
So, with your original regex, enclose it with a pair of parentheses and this should work too.
more importantly... what IDE are you using??
BBEdit: https://forums.livecode.com/viewtopic.php?f=19&t=32121
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”