A bit of regex help needed

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Marco
Posts: 2347
Joined: 27 Jun 2011 15:20

Re: A bit of regex help needed

Post by Marco »

The solution to everything: :arrow: :cup: :wink: :D
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

Andy Petrov
Posts: 10
Joined: 17 Mar 2014 14:40

Re: A bit of regex help needed

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

highend
Posts: 13313
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: A bit of regex help needed

Post by highend »

Dirty..

Code: Select all

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

Code: Select all

$1-$3$5
One of my scripts helped you out? Please donate via Paypal

Andy Petrov
Posts: 10
Joined: 17 Mar 2014 14:40

Re: A bit of regex help needed

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

highend
Posts: 13313
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: A bit of regex help needed

Post 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.
One of my scripts helped you out? Please donate via Paypal

Andy Petrov
Posts: 10
Joined: 17 Mar 2014 14:40

Re: A bit of regex help needed

Post by Andy Petrov »

:kidding: :beer: :beer: :beer:

nerdweed
Posts: 648
Joined: 25 Feb 2012 07:47

Re: A bit of regex help needed

Post 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

highend
Posts: 13313
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: A bit of regex help needed

Post 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");
One of my scripts helped you out? Please donate via Paypal

nerdweed
Posts: 648
Joined: 25 Feb 2012 07:47

Re: A bit of regex help needed

Post by nerdweed »

Thanks highend

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

highend
Posts: 13313
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: A bit of regex help needed

Post by highend »

Don't know what you mean.

Logical or is build with "|"

So a normal pattern looks like
"(one|two|three)"
One of my scripts helped you out? Please donate via Paypal

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: A bit of regex help needed

Post 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?
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

highend
Posts: 13313
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: A bit of regex help needed

Post 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;
One of my scripts helped you out? Please donate via Paypal

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: A bit of regex help needed

Post by SkyFrontier »

Hello, highend.
Thanks much!
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: A bit of regex help needed

Post 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.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

highend
Posts: 13313
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: A bit of regex help needed

Post 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?
One of my scripts helped you out? Please donate via Paypal

Post Reply