Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 10 of 27 total
Thread Regular Expressions - How to?
Sat, Sep 10 2016 2:32 AMPermanent Link

Trinione

How to get patterns from string?

For example, 'This is a sample string with AID:123456 in it and CID:789012.'

How can I get - AID:123456 and CID:789012?

Thx
Sat, Sep 10 2016 12:04 PMPermanent Link

Raul

Team Elevate Team Elevate

On 9/10/2016 2:32 AM, Trinione wrote:
> How to get patterns from string?
> For example, 'This is a sample string with AID:123456 in it and CID:789012.'
> How can I get - AID:123456 and CID:789012?

Based on subject i assume you do not want to parse this using string
functions in EWB.

You could use javascript RegExp
(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions)
- i don't believe EWB exposes this today so use it thru and external
interface declration.

Not sure what your actual expressions are but you could try something
like "\S+\:\S+" as expression to start with.

Raul
Sat, Sep 10 2016 12:48 PMPermanent Link

Raul

Team Elevate Team Elevate

On 9/10/2016 12:04 PM, Raul wrote:
> On 9/10/2016 2:32 AM, Trinione wrote:
>> How to get patterns from string?
>> For example, 'This is a sample string with AID:123456 in it and
>> CID:789012.'
>> How can I get - AID:123456 and CID:789012?
> You could use javascript RegExp
> (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions)
> - i don't believe EWB exposes this today so use it thru and external
> interface declration.

I was playing with some external JS and came up with this - might help
you as starting point

in your external javascript file add this function

function RunRegExec(expr,txt)
{
   var re = new RegExp(expr,"gi");
   var execResult;
   var resultArray = [];
   while ((execResult = re.exec(txt)) !== null)
   {
     resultArray.push(execResult[0]);
   }
   return resultArray;
}


and then declare it as external in EWB

   external function RunRegExec(expr,txt:string):array of string;

and then call it like this (i'm outputting to TMultiLineEdit):

procedure TForm1.Button1Click(Sender: TObject);
var
   regResultArray : array of string;
   i:integer;
begin
   regResultArray := RunRegExec('\S+\:\S+' ,'This is a sample string
with AID:123456 in it and CID:789012.');

   if not assigned(regResultArray) then
      MultiLineEdit1.Lines.Add('Regex found no matches')
   else
   begin
      MultiLineEdit1.Lines.Add('Regex found num of matches  =' +
inttostr(Length(regResultArray)));
      for i:= 0 to Length(regResultArray) - 1 do
      begin
         MultiLineEdit1.Lines.Add(regResultArray[i]);
      end;
   end;
end;


i'm getting this as output (you want to come up with better regex
expression to get rid ot period at the end or such):

Regex found num of matches  =2
AID:123456
CID:789012.

Raul
Sat, Sep 10 2016 2:36 PMPermanent Link

Trinione

Raul wrote:

Thank you very much for the direction. I have implemented and testing various scenarios.

I am wondering if Tim plans on including this as part of EWB?
Sun, Sep 11 2016 11:38 AMPermanent Link

erickengelke

Avatar

Trinione wrote:
>Thank you very much for the direction. I have implemented and testing various scenarios.

You could use the TStringList to do this.  Just set the separator character to colon,
and replace commas with end-of-lines.  Then you have string lookup capabilities all built-in.

Erick
Sun, Sep 11 2016 9:02 PMPermanent Link

Trinione

Erick wrote:
<< You could use the TStringList to do this.  Just set the separator character to colon,
and replace commas with end-of-lines.  Then you have string lookup capabilities all built-in. >>

I needed to add an ending colon to the text.

And example is; 'This is a sample string with AID:123456: in it and CID:789012: It can have several items I need to extract in it CID:2845:.'

Can you provide a short example of how TStringList can be used in place of RegEx in a case like this. I am thinking Pattern Matching via RegEx is the only way to go, so seeing a TStringList way using built-in functions shall be great!
Sun, Sep 11 2016 9:37 PMPermanent Link

erickengelke

Avatar

Trinione wrote:

Erick wrote:
<< You could use the TStringList to do this.  Just set the separator character to colon,
and replace commas with end-of-lines.  Then you have string lookup capabilities all built-in. >>


>And example is; 'This is a sample string with AID:123456: in it and CID:789012: It can have several items I need to extract in it CID:2845:.'

>Can you provide a short example of how TStringList can be used in place of RegEx in a case like this. I am thinking Pattern Matching via RegEx is the only way to go, so seeing a TStringList way using built-in functions shall be great!

Oops,  I didn't see that example.

If you can use spaces instead of the trailing colon, then this works:


procedure TForm1.Button1Click(Sender: TObject);
var
 s : string;
 sl : TStringList;
begin              
 sl := TStringList.Create;
 s := 'aid:234 blk:432 clk:345 ggh:998';
 sl.LineSeparator := ' ';
 sl.NameValueSeparator := ':';
 sl.Text := s;
 ShowMessage( sl.Values[ 'clk']);
end;
Sun, Sep 11 2016 9:39 PMPermanent Link

erickengelke

Avatar

erickengelke wrote:

Erick wrote:
<< You could use the TStringList to do this.  Just set the separator character to colon,
and replace commas with end-of-lines.  Then you have string lookup capabilities all built-in. >>

>Oops,  I didn't see that example.

In my defence, I didn't have my glasses on and thought the colon was a comma.
Sun, Sep 11 2016 9:52 PMPermanent Link

Trinione

Erick wrote:
<< Oops,  I didn't see that example. >>

I had to add the trailing colon after I posted the initial post. Then, all I had to do was a minor mod to the RegEx expression.

<< If you can use spaces instead of the trailing colon, then this works:>>

LOL. I can be sure to add it, however my users cannot be trusted to. The closing colon is easier for me to state as well as for them to include.

Thanks for the code. It may come in handy at some point.
Sun, Sep 11 2016 9:55 PMPermanent Link

Trinione

Erick wrote:
<<  s := 'aid:234 blk:432 clk:345 ggh:998'; >>

Actually, the value would be a message which includes other text. As in:

s := 'I am sending you this message regarding aid:234. I expect that you know it is related to blk:432, which pertains to clk:345. Be sure to have a look at ggh:998 also.'
Page 1 of 3Next Page »
Jump to Page:  1 2 3
Image