A bit of regex help needed

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: A bit of regex help needed

Post by SkyFrontier »

Hi, highend.

The point is to regexreplace any explicitly declared strings so all matching occurrences are replaced.
sc replace is slow for this.
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: 15016
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: A bit of regex help needed

Post by highend »

If
/AAB1
/1ABa
/AB2
/AA
are these explicitly declared strings:

Code: Select all

$test = <<<>>>
/AAaaaa
/AB2aaaa2
/1ABaaaaBCD
/AAB1aaaBCD
>>>;

    $result = "";
    $pattern = "^(/AAB1|/1ABa|/AB2|/AA)(.*)$";
    foreach($item, $test, "<crlf>") {
        $result = $result . "Source: $item<crlf>Result: " . regexreplace($item, $pattern, "$2", 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 »

Thank you very much, highend.
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: 15016
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: A bit of regex help needed

Post by highend »

No problem :beer:

For your reference: Be careful when you use such a pattern:
(/AAB1|/1ABa|/AB2|/AA)
The first match wins!
E.g.:
(/AA|/AAB1|/1ABa|/AB2)
would lead to:
Source: /AAB1aaaBCD
Result: B1aaaBCD
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 »

Yes, it's being a PITA building a working set for this specific task I'm struggling with here...
:beer:
cheers!
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 »

How do I reverse this?

Code: Select all

   $a = "C:\TMP\other.ext | C:\TMP\directory | C:\TMP\image.png | C:\TMP\folder\";

  $gr = regexreplace("$a", "(.*(?=\\.*)\\)(.*\w+)", "$1"); echo $gr;
which outputs:
C:\TMP\other.png | C:\TMP\directory | C:\TMP\image.png | C:\TMP\\

desired:
C:\TMP\ | | C:\TMP\ |
ie, files will have their parents' dirs outputed, folders will be wiped.
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...

FluxTorpedoe
Posts: 906
Joined: 05 Oct 2011 13:15

Re: A bit of regex help needed

Post by FluxTorpedoe »

Hi'

Here's a quick 'n dirty answer with an output like you expected (spaces included); might be a cleaner way though…

Code: Select all

  $a = "C:\TMP\other.ext | C:\TMP\directory | C:\TMP\image.png | C:\TMP\folder\";
  $gr = regexreplace("$a", "(((\w:[^|]+\\)[^\\]+\.\w{2,4})|\w:[^|]+)", "$3"); echo $gr;
You can adjust {2,4} if you have files with extensions with less than 2 or more than 4 characters.

Hope this helps, 8)
Flux

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 Flux!
I was trying to adapt several stuff I found across the web, none worked except the one I used here the way I described, so... many thanks!
I have catch a problem with your solution - trying now to pinpoint what's causing it so further discussion can be done.
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 »

Got it.
It fails on folders named "_comment1_www.site.com_comment2".
Can a delimiter be put so anything other than a-zA-Z beyond the pseudo-extension flags the string as a folder?
-alternatively, how could this work on <crlf>-separated lists? [^\r\n]+)" instead of [^|]+)"?
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...

FluxTorpedoe
Posts: 906
Joined: 05 Oct 2011 13:15

Re: A bit of regex help needed

Post by FluxTorpedoe »

Hmmm, I can't make it fail... :veryconfused: :wink:
Even with paths like this:

Code: Select all

C:\TMP\other.7z | C:\TMP\directory | C:\www.site.com | C:\www.site.com\file.tst | C:\_comment1_www.site.com_comment2 | C:\_comment1_www.site2.com_comment2\file.png | C:\TMP\image.hpng | C:\TMP\folder\
Could you give another example?
SkyFrontier wrote:-alternatively, how could this work on <crlf>-separated lists? [^\r\n]+)" instead of [^|]+)"?
Yep! The "\r" si not necessary though.

Code: Select all

$gr = regexreplace("$a", "(((\w:[^\n]+\\)[^\\]+\.\w{2,4}\b)|\w:[^\n]+\n))", "$3"); echo $gr;
I just added a "\n" at the end of folder paths detection to remove the empty lines (so make sure your list ends with an empty line).
I also blindly added a word boundary ("\b") after the extension in case that helps with your previous problem.

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

Re: A bit of regex help needed

Post by highend »

I guess SkyFrontier meant something like this?
C:\test\_comment1_www.site.com_comment1
Which should be wiped but the output would be:
C:\test\comment1
One of my scripts helped you out? Please donate via Paypal

FluxTorpedoe
Posts: 906
Joined: 05 Oct 2011 13:15

Re: A bit of regex help needed

Post by FluxTorpedoe »

Ohhh, right.

Here it is for pipe-separated lists (with or without spaces around pipes):

Code: Select all

$gr = regexreplace("$a |", "(((\w:[^|]+\\)[^\\]+\.\w{2,4}(?= *\|))|\w:[^|]+)", "$3"); echo $gr;
For <crlf> separated lists:

Code: Select all

$gr = regexreplace("$a<crlf>", "(((\w:[^\n]+\\)[^\\]+\.\w{2,4}$)|\w:[^\n]+\n)", "$3"); echo $gr;
Note: in both cases, the string to search must end with its delimiter (i.e. "$a |" or "$a<crlf>"). Much easier in regex than trying to also match end of string.

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

Re: A bit of regex help needed

Post by SkyFrontier »

Thanks much, guys. Much appreciated.

Is regex the way to reduce a list like
input:
c:\test\sub\file.txt
c:\stuff\soft\FirefoxPortable\Other
c:\stuff\soft\App
c:\stuff\soft\FirefoxPortable\FirefoxPortable.exe
c:\test\sub2\Other\prog.exe

to common base elements like
output:
c:\stuff\soft\
c:\test\

-but NEVER drive roots, if any?
input:
c:\stuff\soft\
c:\test\
>>
c:\ <- NOT allowed!
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...

Garonne
Posts: 195
Joined: 21 Apr 2015 19:10
Location: Win11, 100% Scaling
Contact:

Re: A bit of regex help needed

Post by Garonne »

SkyFrontier wrote:Thanks much, guys. Much appreciated.

Is regex the way to reduce a list like
input:
[...]

to common base elements like
output:
[...]

-but NEVER drive roots, if any?
[...]
Honestly I don't think so, since RegEx does take each input separately and isn't able to compare it to other (similar) inputs in any way.
RegEx + Scripting: Yeah, that might do, but I'm not good enough into scripting to help you there.
RegEx on it's own won't be able to finish that job.

eil
Posts: 1911
Joined: 13 Jan 2011 19:44

Re: A bit of regex help needed

Post by eil »

need help with regex for batch rename. occasionally i have a need to batch rename many files deleting/replacing some part/word from base. no any smartness needed here, just strict stated word present in all bases and what to do with it. examples:
-delete- "Intr">""
!!RotrIntr_Patch.big > !!Rotr_Patch.big
!RotrIntr_English.big > !Rotr_English.big
-replace- "Intr">"Ex"
!!RotrIntr_Patch.big > !!RotrEx_Patch.big
!RotrIntr_English.big > !RotrEx_English.big

hope this suits the topic тв someone have time to help. :)
Win 7 SP1 x64 100% 1366x768|1900x1080

Post Reply