Page 4 of 7

Re: A bit of regex help needed

Posted: 24 Mar 2014 16:47
by Marco
The solution to everything: :arrow: :cup: :wink: :D

Re: A bit of regex help needed

Posted: 08 May 2014 12:07
by Andy Petrov
Hi, dear members!
Could someone please help me with regex?

Input:
FixedPart Variable1 Variable2.ext
Output:
FixedPart-Variable1Variable2.ext

Replace first space with - and trim second space.
TIA :beer:

Re: A bit of regex help needed

Posted: 08 May 2014 12:21
by highend
Dirty..

Code: Select all

(^.*?)(\s)(.*?)(\s)(.*)
Replace with:

Code: Select all

$1-$3$5

Re: A bit of regex help needed

Posted: 08 May 2014 14:18
by Andy Petrov
highend, thank you. It works :D
And how to make a more universal exp?

Input:
FixedPart Variable1.ext
FixedPart Variable1 Variable2.ext
FixedPart Variable1 Variable2 Variable3.ext

Output:
FixedPart-Variable1.ext
FixedPart-Variable1Variable2.ext
FixedPart-Variable1Variable2Variable3.ext


Replace first space with - and trim other spaces.

:beer: :beer:

Re: A bit of regex help needed

Posted: 08 May 2014 14:46
by highend
In one turn? Sorry, don't know and no time to think about.

Just use a simple script:

Code: Select all

    foreach($item, <get SelectedItemsNames |>, "|") {
        renameitem(regexreplace(regexreplace($item, "(^.*?)\s", "$1-"), "\s"), $item, "3");
    }
Reminder for myself: Coding is often faster than spending too much time on thinking about the problem...

All SELECTED files will be renamed according to your (last) wish.

Re: A bit of regex help needed

Posted: 08 May 2014 15:07
by Andy Petrov
:kidding: :beer: :beer: :beer:

Re: A bit of regex help needed

Posted: 17 May 2014 14:05
by nerdweed
From the tags.dat file, I am trying to read this
Read Filename and Extra Tag 4 for all those entries which has the tag Images

$data = RegExMatches ("$data", "(.+)\|0+\|.*Images.*\|.*\|.*\|(.*)\|.*\|", <crlf>) ;

The containers in this store this data however, RegExMatches returns the whole row.

$data = RegExReplace ("$data", "(.+)\|0+\|.*Images.*\|.*\|.*\|(.*)\|.*\|", "$1|$2") ;

If I try RegExRepalce, it returns the header data as well as well as files without Images tag.

Combination of both gives the desired output. Any better way to do this

Re: A bit of regex help needed

Posted: 17 May 2014 15:32
by highend
You shouldn't use needy matches only if really needed.
Apart from that regexmatches isn't able to capture specific parts via backreferences!

Imho you still have to combine them...

Code: Select all

    text regexreplace(regexmatches(readfile("<xydata>\tag.dat"), "^[A-Z]:.*?\|0+\|.*?images.*?$", "<crlf>"), "^([A-Z]:.*?)\|0+\|.*?\|.*?\|.*?\|.*?\|(.*?)\|", "$1|$2");

Re: A bit of regex help needed

Posted: 17 May 2014 17:38
by nerdweed
Thanks highend

Didn't knew that ? is used for lazy method.

Re: A bit of regex help needed

Posted: 17 May 2014 17:46
by highend
Don't know what you mean.

Logical or is build with "|"

So a normal pattern looks like
"(one|two|three)"

Re: A bit of regex help needed

Posted: 05 Jun 2015 13:37
by SkyFrontier
Help on regexreplace, please.

These are samples:
AaaaaB - B must go
Aaaaa/AB2 - /AB2 must go
AaaaBCD/1AB2 - BCD/1AB2 must go

IOW, preserve everything before "/" BUT all the immediate left upper cased letters.
How?

Re: A bit of regex help needed

Posted: 05 Jun 2015 14:14
by highend

Code: Select all

$test = <<<>>>
AaaaaB
Aaaaa/AB2
AaaaBCD/1AB2
>>>;

    $result = "";
    $pattern = "^([A-Z][a-z]+)(/|[A-Z])(.*)$";
    foreach($item, $test, "<crlf>") {
        $result = $result . "Source: $item<crlf>Result: " . regexreplace($item, $pattern, "$1", 1) . "<crlf 2>";
    }
    text $result;

Re: A bit of regex help needed

Posted: 05 Jun 2015 14:46
by SkyFrontier
Hello, highend.
Thanks much!

Re: A bit of regex help needed

Posted: 06 Jun 2015 11:49
by SkyFrontier
Tried many variations but couldn't find a way to match the opposite way, this time with EXACT matches

/AAaaaa - aaaa
/AB2aaaa2 - aaaa2
/1ABaaaaBCD - aaaBCD
/AAB1aaaBCD - aaaBCD

-in this case, I'll have a line specifying that "AA", another line for "AB2", another for "/1ABa" (yes, a lower case there) and another for "/AAB1"- all these strings must go.
They appear like
/AAaaaa/AB2aaaa2
and will be output'ed like
aaaa
aaaa2

Thanks in advance.

Re: A bit of regex help needed

Posted: 06 Jun 2015 12:04
by highend
You can't catch

/1ABaaaaBCD - aaaBCD

with the same pattern you'd catch the other three ones... At least I don't see any logic behind it (no explicit number of characters to catch / delete).

Or does it follow the logic:
If a line begins with "/<a digit>" than capture the first 5 (including the "/") characters instead of only the first 4?