Help - Search - Members - Calendar
Full Version: Using regex to return a substring
Mp3tag Forums > Mp3tag - International > Support
egd
anyone have a regex to return a substring preceding or following the first occurrence of a delimiter?
DetlevD
QUOTE (egd @ Sep 9 2008, 00:53) *
anyone have a regex to return a substring preceding or following the first occurrence of a delimiter?

Any example?
What delimiter?

DD.20080909.0611.CEST
egd
hello : world
hello:world
hello: world
hello :world

In each case I would love regex's to return "hello" or "world", depending on requirements. The delimiter could be something else also, but let's assume it could be any of ":;,"
DetlevD
$regexp('hello : world',^(.+?) *'[:;,]+' *(.+?)$,$1) ==> 'hello'
$regexp('hello : world',^(.+?) *'[:;,]+' *(.+?)$,$2) ==> 'world'

$regexp('hello:world',^(.+?) *'[:;,]+' *(.+?)$,$1) ==> 'hello'
$regexp('hello:world',^(.+?) *'[:;,]+' *(.+?)$,$2) ==> 'world'

$regexp('hello :world',^(.+?) *'[:;,]+' *(.+?)$,$1) ==> 'hello'
$regexp('hello :world',^(.+?) *'[:;,]+' *(.+?)$,$2) ==> 'world'

$regexp('hello: world',^(.+?) *'[:;,]+' *(.+?)$,$1) ==> 'hello'
$regexp('hello: world',^(.+?) *'[:;,]+' *(.+?)$,$2) ==> 'world'

DD.20080909.1408.CEST
egd
thank you. Forgive my ignorance, but what would the "Regular expression" and "Replace matches with" entries look like if this were turned into an action?
DetlevD
QUOTE (egd @ Sep 9 2008, 22:34) *
thank you. Forgive my ignorance, but what would the "Regular expression" and "Replace matches with" entries look like if this were turned into an action?

I leave this task to you: transferring of these $regexp() expressions into Mp3tag action forms.
You may have to check the case when there is no match at all (if there is no delimiter).

DD.20080910.0710.CEST
egd
QUOTE (DetlevD @ Sep 10 2008, 15:11) *
I leave this task to you: transferring of these $regexp() expressions into Mp3tag action forms.
You may have to check the case when there is no match at all (if there is no delimiter).

DD.20080910.0710.CEST


I'd tried it and got no result, hence my asking. What I did was as follows:
For $regexp('hello : world',^(.+?) *'[:;,]+' *(.+?)$,$1) ==> 'hello'

Regular expression: ^(.+?) *'[:;,]+'
Replace matches with: *(.+?)$,$1)
DetlevD
QUOTE (egd @ Sep 10 2008, 07:37) *
I'd tried it and got no result, hence my asking. ...


Look at http://help.mp3tag.de/en/main_scripting.html
QUOTE
$regexp(what,expr,repl)
Replaces the pattern specified by the regular expression expr in the string what by repl. The fourth optional parameter enables ignore case (1) or disables the ignore case setting (0). Please note that you have to escape comma and other special characters in expr (see end of this document).

$regexp('hello : world',^(.+?) *'[:;,]+' *(.+?)$,$1)

DD.20080910.0907.CEST
egd
QUOTE (DetlevD @ Sep 10 2008, 17:07) *
Look at http://help.mp3tag.de/en/main_scripting.html

$regexp('hello : world',^(.+?) *'[:;,]+' *(.+?)$,$1)

DD.20080910.0907.CEST


Hi DetlevD

Thanks for getting back to me. I've tried the above at http://www.regextester.com/ using:
regex: ^(.+?) *'[;]+' *(.+?)$
Test on Text: hello : world
Replace with: $1

and it returns "No match" sad.gif
egd
I've gotten it working smile.gif

seems one has to to remove the "'" on either side of '[:;,]+' for the expression to evaluate.

thanks again
DetlevD
QUOTE (egd @ Sep 10 2008, 15:21) *
I've gotten it working smile.gif

I'm glad to hear this!
QUOTE (egd @ Sep 10 2008, 15:21) *
... seems one has to to remove the "'" on either side of '[:;,]+' for the expression to evaluate. ...

Ah, yes, the single quotation mark ' is used within the $regexp() function as an escape character to prevent such special characters like e. g. square brackets or commas from misinterpreting by the function parameter parser.
The Mp3tag regex action form does not need this extra escaping.

DD.20080910.1652.CEST
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2014 Invision Power Services, Inc.