Page 1 of 1

Using regexpr and () to group and extract substrings

Posted: Tue Sep 21, 2010 8:50 am
by danyal711
I want to do: sys_RegexFind( mystring, ' \(Disc (.?)\)' )

and then later use $1  => which should equal the match of (.?)

Or somehow get the value of the item I matched and use it later.

-----------

What I am trying to do is the following: I have a large # of flac files that have an album field like "Some Album (Disc X)". I want to write a script which finds (Disc X), deletes it from the album field and adds DISCNUM X as one of the custom fields in the tag. The modification part of the Album tag is straightforward as is adding a new tag (DISCNUM) however I need to get the value of X from the regex so I can use it when creating the new field.

Re: Using regexpr and () to group and extract substrings

Posted: Tue Sep 21, 2010 10:31 am
by jtclipper
I will add another system function to extract the match of the regex find and return it in the next beta (3).
If you are in a hurry this can also be done by using Pos() and some manuall processing of the data.

Re: Using regexpr and () to group and extract substrings

Posted: Wed Sep 22, 2010 3:40 am
by GHammer
Just as an aside, I find this very handy when working with regex. I don't do enough to keep it straight in my head.

http://msdn.microsoft.com/en-us/library/az24scfc.aspx

Re: Using regexpr and () to group and extract substrings

Posted: Sun Sep 26, 2010 5:49 pm
by jtclipper
Beta 3 has a new regex function

Code: Select all

function sys_RegexFindStr( sSource, sRegexExpr: string; iIdx: integer ): string;
Example

Code: Select all

program test;

var
  sTmp: string;
begin

  sTmp := '172.16.225.101';
  ShowMessage( sys_RegexFindStr( sTmp, '(\d+)\.(\d+)\.(\d+)\.(\d+)',  4 ) ); //<-- will return 101

end.

Re: Using regexpr and () to group and extract substrings

Posted: Fri Oct 01, 2010 7:04 am
by danyal711
Worked like a charm. Thanks for your fast response! Another question I have however is I need to set the DISCNUMBER field (and probably DISCTOTAL as well). Is there any way to do this?

I should mention I need to set DISC related information for FLAC and MP3

Re: Using regexpr and () to group and extract substrings

Posted: Fri Oct 01, 2010 9:58 am
by jtclipper
The basic idea is as follows:

Code: Select all

program test;
begin
  if not tg_Init then exit;
  repeat
    tg_LoadFile;
    gTag.ExtraFieldSet( 'DISCNUMBER', '3' )
    gTag.SaveToFile( 0, false, '', false);
  until not tg_Skip;
end.
The question is which hardware/software player will you use because some may or may not use that info.

Re: Using regexpr and () to group and extract substrings

Posted: Tue Oct 26, 2010 5:33 am
by atagal
Dimitris, how to replace all commas to dots in string?
"a, b, c, d" replace to "a. b. c. g"

Re: Using regexpr and () to group and extract substrings

Posted: Tue Oct 26, 2010 7:59 am
by jtclipper
You can use the following method

Code: Select all

program test;
var
  sTmp: string;
begin
  sTmp := '10.10.10.50';
  sTmp := AnsiReplacetext( sTmp, '.', ',' );
  ShowMessage( stmp );
end.