A bit of regex help needed
-
SkyFrontier
- Posts: 2341
- Joined: 04 Jan 2010 14:27
- Location: Pasárgada (eu vou!)
Re: A bit of regex help needed
Hi, highend.
The point is to regexreplace any explicitly declared strings so all matching occurrences are replaced.
sc replace is slow for this.
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...
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
If
are these explicitly declared strings:/AAB1
/1ABa
/AB2
/AA
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
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...
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
No problem
For your reference: Be careful when you use such a pattern:
E.g.:
For your reference: Be careful when you use such a pattern:
The first match wins!(/AAB1|/1ABa|/AB2|/AA)
E.g.:
would lead to:(/AA|/AAB1|/1ABa|/AB2)
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
Yes, it's being a PITA building a working set for this specific task I'm struggling with here...

cheers!
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...
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
How do I reverse this?
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.
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;
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...
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
Hi'
Here's a quick 'n dirty answer with an output like you expected (spaces included); might be a cleaner way though…
You can adjust {2,4} if you have files with extensions with less than 2 or more than 4 characters.
Hope this helps,
Flux
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;Hope this helps,
Flux
• Scripts: Session Manager
| SlideShow | Collection Manager | Power Launcher | Akelpad syntax highlighting | ...
-
SkyFrontier
- Posts: 2341
- Joined: 04 Jan 2010 14:27
- Location: Pasárgada (eu vou!)
Re: A bit of regex help needed
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.
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...
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
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 [^|]+)"?
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...
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
Hmmm, I can't make it fail...
Even with paths like this:Could you give another example?
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.
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\Yep! The "\r" si not necessary though.SkyFrontier wrote:-alternatively, how could this work on <crlf>-separated lists? [^\r\n]+)" instead of [^|]+)"?
Code: Select all
$gr = regexreplace("$a", "(((\w:[^\n]+\\)[^\\]+\.\w{2,4}\b)|\w:[^\n]+\n))", "$3"); echo $gr;I also blindly added a word boundary ("\b") after the extension in case that helps with your previous problem.
• Scripts: Session Manager
| SlideShow | Collection Manager | Power Launcher | Akelpad syntax highlighting | ...
-
highend
- Posts: 15016
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: A bit of regex help needed
I guess SkyFrontier meant something like this?
Which should be wiped but the output would be:C:\test\_comment1_www.site.com_comment1
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
Ohhh, right.
Here it is for pipe-separated lists (with or without spaces around pipes):
For <crlf> separated lists:
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.
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;Code: Select all
$gr = regexreplace("$a<crlf>", "(((\w:[^\n]+\\)[^\\]+\.\w{2,4}$)|\w:[^\n]+\n)", "$3"); echo $gr;• Scripts: Session Manager
| SlideShow | Collection Manager | Power Launcher | Akelpad syntax highlighting | ...
-
SkyFrontier
- Posts: 2341
- Joined: 04 Jan 2010 14:27
- Location: Pasárgada (eu vou!)
Re: A bit of regex help needed
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!
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...
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
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.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?
[...]
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
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.
-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
XYplorer Beta Club