#1 2008-02-28 04:56

krtek
Senior Member
From: Łódź (Poland)
Registered: 2008-02-21
Posts: 262

ReplaceRegEx and MatchesRegEx functions in PascalScript

Hi again!
Maybe a silly question, but it's not too easy to find out how PascalScript functions work just by their names and parameters tongue

Do ReplaceRegEx and MatchesRegEx functions only simulate RegEx rule within PascalScript or maybe they can take input/output of the RegEx rule which was executed before starting PascalScript rule?
The longer I search through the forum, the more I think that it's the first option and that ReplaceRegEx and MatchesRegEx functions have NOTHING in common with any of RegEx rules in rules table. Am I right?

Last edited by krtek (2008-02-28 06:29)


Regular Expressions are not as hard to understand as you may think. Check ReNamer's manual or nice Regular Expressions tutorial for more info and start to use full power of applications that use them (like ReNamer, Mp3Tag and so on).

Offline

#2 2008-02-28 06:11

krtek
Senior Member
From: Łódź (Poland)
Registered: 2008-02-21
Posts: 262

Re: ReplaceRegEx and MatchesRegEx functions in PascalScript

Ufff, I think I figured it out.
The answer is YES, these functions have nothing in common with any of other rules.


function ReplaceRegEx(const Input, Find, Replace: WideString; const CaseSensitive, UseSubstitution: Boolean): WideString;

description of parameters:
Input - string to be processed by RegEx engine, eg. FileName
Find  - equivalent for the field 'Expression' from the RegEx rule dialog
Replace - well... 'Replace' from the RegEx rule dialog
CaseSensitive
UseSubstitution - determines whether use references in result or not. If it's set to True, function will change $1-$9 in 'Replace' string with the corresponding parts of 'Find' expression, eg. (.+). If it's set to False, function will print $1 as it is (eg. "My_Picture_$1" instead of "My_Picture_365") to the result string.

Last edited by krtek (2008-02-28 06:46)


Regular Expressions are not as hard to understand as you may think. Check ReNamer's manual or nice Regular Expressions tutorial for more info and start to use full power of applications that use them (like ReNamer, Mp3Tag and so on).

Offline

#3 2008-02-28 06:46

krtek
Senior Member
From: Łódź (Poland)
Registered: 2008-02-21
Posts: 262

Re: ReplaceRegEx and MatchesRegEx functions in PascalScript

I thought that function MatchesRegEx does the similiar job with that difference that it stores references (every separate match, which you could refer to using $1-$9) in the array of strings. But it does something completely different. It stores only the whole match of the RegEx...
Isn't it a bug? What could be the purpose for that function to be TStringsArray type if the input type is WideString?



I've tried to find out what does the MatchesRegEx function with that very amateur piece of code:

var 
My_Matches : TStringsArray;
My_Message : WideString;
i : integer;

begin

My_Matches:=MatchesRegEx(FileName, '(.+) (.+)' , false);


//showmessage(My_Matches[0]);
//showmessage(inttostr(high(My_Matches)));

My_Message:=''
for i:=0 to high(My_Matches) do
  My_Message:=My_Message + #10 + My_Matches[i]; 
showmessage(My_Message);
end.

And with the same filename ("some name.txt") and the same regular expression ReplaceRegEx function as well as RegEx rule did split the filename into $1-$2 pieces.
But MatchesRegEx function always fill only first field of array My_Matches with the whole match of the regex. I've checked it by substituting '(.+) (.+)' with (name).
Maybe I do something wrong?
Do anyone know what is all that about?

Ps. I've tried this on the newest beta version (17 Feb) and on the last stable version (5.20, 30 Jan)

Last edited by krtek (2008-02-28 06:50)


Regular Expressions are not as hard to understand as you may think. Check ReNamer's manual or nice Regular Expressions tutorial for more info and start to use full power of applications that use them (like ReNamer, Mp3Tag and so on).

Offline

#4 2008-03-04 21:13

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,087

Re: ReplaceRegEx and MatchesRegEx functions in PascalScript

No, it's not a bug. Let me explain now...

If you run the script below - you will get all your answers big_smile

const
  SAMPLE = 'one two three';
var 
  I: integer;
  Message: WideString;
  Matches: TStringsArray;
begin
  Matches := MatchesRegEx(SAMPLE, '\w+', False);
  Message := 'FOUND '+IntToStr(Length(Matches))+' MATCHES:';
  for I:=0 to Length(Matches)-1 do
    Message := Message + #10 + Matches[i]; 
  WideShowMessage(Message);
end.

MatchesRegEx function returns an array of full matches, which matched the entire expression, not the sub-patterns. I didn't want to make it too complicated by returning the "Array of Array of String", which is too confusing, but that is required to return all full matches and their associated sub-patterns.

Offline

#5 2008-03-08 08:06

krtek
Senior Member
From: Łódź (Poland)
Registered: 2008-02-21
Posts: 262

Re: ReplaceRegEx and MatchesRegEx functions in PascalScript

Thanks for that explanation. It threw a beam of light on regex functions in pascalscript smile
I had a feeling that Help and Support was better place for that question than Bugs roll

And now, when I know what both Regex functions are doing I'm wondering if there is any easy way to assign sub-patterns of regex ($1,$2 and so on...) to an array (eg. patterns[1]=$1, patterns[2]=$2 and so on).

I use regex's mostly as masks for whole oldname, eg. (\d\d)(.+)bla(.+)
That means regex will find only one match. But the match is not important for me, as it always is Filename... tongue
Important parts are in bracket's. Regex's are wonderful for splitting names into parts (in PacalScript you may need nice piece of code for split long  name into several parts <especially when you're novice to it tongue >, in regex you can do it with one line).
But regex's can't do everything. Sometimes you need IF-THEN-ELSE, or Uppercase just a part of filename... And that's where Pascal comes into action.
I hope there is already an easy way to combine power of regex and Pascal.
But what if not?
I understand the problem with "Array of arrays" which definitely would be confusing for anyone who doesn't need sub-patterns...
Maybe another function which would return only FIRST match and it's subpatterns (or user defined match - then you could use it in a loop to produce array of arrays if anyone would need that)?

Eg. for regex: (\d\d)(.+)19
and Filename: 05name19.12feb1982
it would return:
match[0]=05name19
match[1]=05
match[2]=name

What do you think about it?

Last edited by krtek (2008-03-08 08:08)


Regular Expressions are not as hard to understand as you may think. Check ReNamer's manual or nice Regular Expressions tutorial for more info and start to use full power of applications that use them (like ReNamer, Mp3Tag and so on).

Offline

#6 2008-03-09 07:29

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,087

Re: ReplaceRegEx and MatchesRegEx functions in PascalScript

I was thinking of the best way to add this functionality, and here is what I came too...

function SubMatchesRegEx(const Input, Find: WideString; const CaseSensitive: Boolean): TStringsArray;

This functions is very similar to MatchesRegEx, but instead of returning full expression matches it will return an array of sub-expression matches for the first full expression match. For example, SubMatchesRegEx('one two three', '(.*) (.*) (.*)') will return an array ['one', 'two', 'three']. In this way, it can easily be combined with MatchesRegEx function, to allow users to find all global matches, and then parse those matches through SubMatchesRegEx function to find individual sub-expression matches of each global match.

It only sound complicated, but it's very easy to use wink

As usual, it is available from here: ReNamerBeta.zip

Offline

#7 2008-03-09 16:55

krtek
Senior Member
From: Łódź (Poland)
Registered: 2008-02-21
Posts: 262

Re: ReplaceRegEx and MatchesRegEx functions in PascalScript

Thanks! That was quick! And works perfectly.
Firstly I thought it would be nice to have subpaterns in array numbered in the way that RegEx does it (Subpaterns[1]=$1, Subpaterns[2]=$2) and to have whole match stored in Subpaterns[0] for the possibility of checking whether it match whole filename or not. But it would only cause mistakes and lead to misuse (especially in FOR loops).

Your version is better big_smile
You can always check the whole match with MatchesRegEx function (as in code below).
And it's not hard to remember that:
$1=SubPatern[0]
$2=SubPatern[1] and so on...


var
  vUserInput : string;
  Matches, SubPaterns : TStringsArray;
begin
  vUserInput := 'Don''t bother. Just answer the question :-)';
  Matches:=MatchesRegEx(Filename, '(\d\d) name(.+)', False);
  if Length(Matches) <= 0 then exit;
  if Matches[0] <> Filename then 
     if InputQuery('Not a full match', 'RegEx didn''t match the full filename.' +#10+ 'Would you like to continue?', vUserInput)=false then exit;
  SubPaterns:=SubMatchesRegEx(Matches[0],'(\d\d) name(.+)',False);

//  rest of code...
end.

BTW, is there any function that I could use instead of InputQuery? Obviously, I don't need the input text field and it looks wierd, when you're prompt to input text, when it's only YES/NO question (YES/NO buttons would be also better then OK/Cancel).

Cheers,
Konrad

Last edited by krtek (2008-03-09 17:06)


Regular Expressions are not as hard to understand as you may think. Check ReNamer's manual or nice Regular Expressions tutorial for more info and start to use full power of applications that use them (like ReNamer, Mp3Tag and so on).

Offline

#8 2008-03-09 18:57

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,087

Re: ReplaceRegEx and MatchesRegEx functions in PascalScript

Glad that you like the SubMatchesRegEx functions big_smile

Regarding Yes/No dialog... It might be a good idea actually. I'll see what I can do...

Offline

#9 2008-03-09 19:29

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,087

Re: ReplaceRegEx and MatchesRegEx functions in PascalScript

Trying to choose the right name for this new function:

MessageYesNo, ShowMessageYesNo, DialogYesNo, ConfirmMessage, etc.... ??

Consider that other dialogs may be added in the future, and the name should provide easy fit for future dialogs.

P.S. For the future, please, put questions/suggestions not directly related to the current topic in the new topic.

Offline

#10 2008-03-09 21:09

krtek
Senior Member
From: Łódź (Poland)
Registered: 2008-02-21
Posts: 262

Re: ReplaceRegEx and MatchesRegEx functions in PascalScript

I like DialogYesNo most.


I don't know how it is implemented, but maybe it can be simply Dialog with customizable buttons with user-defined names?
Usage could be:
Dialog('Туалет', 'Вы действительно хотите отлить?', 'Да', 'Нет', 'Не знаю', 'Может позже')
It would demand dynamic quantity of parameters...

It can be not worth the effort. Obviously Yes/No is the most important one.

Ps. My fault roll I'll try to remember next time.

Last edited by krtek (2008-03-09 21:19)


Regular Expressions are not as hard to understand as you may think. Check ReNamer's manual or nice Regular Expressions tutorial for more info and start to use full power of applications that use them (like ReNamer, Mp3Tag and so on).

Offline

Board footer

Powered by FluxBB