Using regexpr and () to group and extract substrings

Use this forum for everything concerning script usage
Post Reply
danyal711
Newbie
Newbie
Posts: 2
Joined: Tue Sep 21, 2010 8:47 am

Using regexpr and () to group and extract substrings

Post 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.

User avatar
jtclipper
Administrator
Administrator
Posts: 768
Joined: Tue Aug 10, 2010 12:04 pm

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

Post 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.
Dimitris

GHammer
Newbie
Newbie
Posts: 27
Joined: Mon Sep 13, 2010 4:59 pm
Location: China

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

Post 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

User avatar
jtclipper
Administrator
Administrator
Posts: 768
Joined: Tue Aug 10, 2010 12:04 pm

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

Post 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.
Dimitris

danyal711
Newbie
Newbie
Posts: 2
Joined: Tue Sep 21, 2010 8:47 am

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

Post 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
Last edited by danyal711 on Fri Oct 01, 2010 7:07 am, edited 1 time in total.

User avatar
jtclipper
Administrator
Administrator
Posts: 768
Joined: Tue Aug 10, 2010 12:04 pm

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

Post 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.
Dimitris

atagal
Newbie
Newbie
Posts: 34
Joined: Tue Oct 26, 2010 12:08 am

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

Post by atagal »

Dimitris, how to replace all commas to dots in string?
"a, b, c, d" replace to "a. b. c. g"

User avatar
jtclipper
Administrator
Administrator
Posts: 768
Joined: Tue Aug 10, 2010 12:04 pm

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

Post 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.
Dimitris

Post Reply