Page 5 of 7

Re: A bit of regex help needed

Posted: 06 Jun 2015 12:17
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.

Re: A bit of regex help needed

Posted: 06 Jun 2015 12:19
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;

Re: A bit of regex help needed

Posted: 06 Jun 2015 12:31
by SkyFrontier
Thank you very much, highend.

Re: A bit of regex help needed

Posted: 06 Jun 2015 12:44
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

Re: A bit of regex help needed

Posted: 06 Jun 2015 12:59
by SkyFrontier
Yes, it's being a PITA building a working set for this specific task I'm struggling with here...
:beer:
cheers!

Re: A bit of regex help needed

Posted: 17 Jun 2015 04:39
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.

Re: A bit of regex help needed

Posted: 17 Jun 2015 12:25
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

Re: A bit of regex help needed

Posted: 17 Jun 2015 16:39
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.

Re: A bit of regex help needed

Posted: 17 Jun 2015 17:57
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 [^|]+)"?

Re: A bit of regex help needed

Posted: 18 Jun 2015 03:07
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.

Re: A bit of regex help needed

Posted: 18 Jun 2015 08:39
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

Re: A bit of regex help needed

Posted: 18 Jun 2015 10:12
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.

Re: A bit of regex help needed

Posted: 19 Jun 2015 01:04
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!

Re: A bit of regex help needed

Posted: 19 Jun 2015 06:20
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.

Re: A bit of regex help needed

Posted: 20 Jun 2015 13:09
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. :)