Author Topic: Search routine  (Read 24003 times)

Mathias (Author)

  • Administrator
  • Elite Member
  • *****
  • Posts: 3602
    • View Profile
    • Multi Commander
Re: Search routine
« Reply #25 on: August 08, 2013, 17:25:07 »
Is it just my lack of skill or StrReplace won't accept a regex at all?
StrReplace replaces substrings.

Ulfhednar

  • Contributor
  • Elite Member
  • *****
  • Posts: 445
    • View Profile
Re: Search routine
« Reply #26 on: August 09, 2013, 18:33:40 »
OK thanks Mathias.
I will have to do more research.  ;)
I can envisage a multiple file/folder sequence with several routines employed - be nice to get it to work. :P

& OT (but still about regexs!)
Is the greyed out regex box in the mass rename dialog temporary?

Mathias (Author)

  • Administrator
  • Elite Member
  • *****
  • Posts: 3602
    • View Profile
    • Multi Commander
Re: Search routine
« Reply #27 on: August 10, 2013, 15:50:55 »
& OT (but still about regexs!)
Is the greyed out regex box in the mass rename dialog temporary?

It is planed to support regex in the search part in the search and replace section. But I have not had the time too add it.


Ulfhednar

  • Contributor
  • Elite Member
  • *****
  • Posts: 445
    • View Profile
Re: Search routine
« Reply #28 on: August 11, 2013, 18:34:17 »
 
It is planed to support regex in the search part in the search and replace section. But I have not had the time too add it.

Thanks for the update Mathias.  I will continue tearing my hair out with scripting trials while I wait.  :D

Mathias (Author)

  • Administrator
  • Elite Member
  • *****
  • Posts: 3602
    • View Profile
    • Multi Commander
Re: Search routine
« Reply #29 on: August 17, 2013, 17:50:03 »
It is planed to support regex in the search part in the search and replace section. But I have not had the time too add it.

Thanks for the update Mathias.  I will continue tearing my hair out with scripting trials while I wait.  :D

Build 1469 do now support it

Ulfhednar

  • Contributor
  • Elite Member
  • *****
  • Posts: 445
    • View Profile
Re: Search routine
« Reply #30 on: August 18, 2013, 15:29:31 »
I have begun looking at the MC regex function.
I wonder if this is not an acceptable regex -
Code: [Select]
((?:[0-9]+)+)ie: -  find numbers, can be any number of numerals in a group.
Same for letters
Code: [Select]
((?:[a-zA-Z]+)+)
MC doesn't seem to accept this format. 
I see your example
Code: [Select]
<str> StrRegExpReplace(<str>, <regex>, <replaceWith>, [int startPos] )
example

@var $str = "This.String.A02B22.SUBString-ZZZZ";
@var $re = ".A[0-9][0-9]B[0-9][0-9]."";
@var $result = StrRegExpReplace($str, $re, "XX");
 // result is This.StringXXSUBString-ZZZZ

 - specifies quantity of individual characters.  'Hungry' expression is defined differently in MC?

For my experiment I had been thinking of changing a folder date format, e.g. :-
10-05-1989 -> 10.05.1989
Code: [Select]
((?:[0-9]+)+)-((?:[0-9]+)+)   ->    ((?:[0-9]+)+).((?:[0-9]+)+)or
10_May_1989 -> 10 May 1989
Code: [Select]
((?:[0-9]+)+)_((?:[a-zA-Z]+)+)_((?:[0-9]+)+) ->     ((?:[0-9]+)+) ((?:[a-zA-Z]+)+) ((?:[0-9]+)+)
   
I tried: -
Code: [Select]
@var $arr = GetSourceSelectedPaths();
@var $items = arrayCount($arr);
@var $CurrentNameFullPath;
@var $OrgName;
@var $NewName;
@var $re = "((?:[0-9]+)+)-((?:[0-9]+)+)-((?:[0-9]+)+)";
@var $str = $OrgName;
@var $n = 0;

for( $n = 0; $n < $items; $n++ )
{
  $CurrentNameFullPath = $arr[ $n ];
  $OrgName = PathGetNamePart( $CurrentNameFullPath );
  $NewName = StrRegExpReplace($str, $re, ".");
 
  RenameFile( $arr, $NewName, "SILENT" );
  }

But I get - Error : -23 => Parameter wrong type or in the mass rename - 'invalid characters' msg
I had the regex's working  outside MC - but they don't work in the mass rename or the script (yet)   :-[


« Last Edit: August 18, 2013, 16:20:52 by Ulfhednar »

Mathias (Author)

  • Administrator
  • Elite Member
  • *****
  • Posts: 3602
    • View Profile
    • Multi Commander
Re: Search routine
« Reply #31 on: August 18, 2013, 16:00:09 »
I have begun looking at the MC regex function.
I wonder if this is not an acceptable regex -
Code: [Select]
((?:[0-9]+)+)
Don't know. I'm not a regex expert. I just use the regex libraries that is included in the C++11 standard and that regex engine should be mostly perl regex compatible
MC doesn't seem to accept this format. 
I see your example
Code: [Select]
<str> StrRegExpReplace(<str>, <regex>, <replaceWith>, [int startPos] )
example

@var $str = "This.String.A02B22.SUBString-ZZZZ";
@var $re = ".A[0-9][0-9]B[0-9][0-9]."";
@var $result = StrRegExpReplace($str, $re, "XX");
 // result is This.StringXXSUBString-ZZZZ

 - specifies quantity of individual characters.  'Hungry' expression is defined differently in MC?
Hungry ?  The above example works. It is takes from my automated unittests.

For my experiment I had been thinking of changing a folder date format, e.g. :-
10-05-1989 -> 10.05.1989
Code: [Select]
((?:[0-9]+)+)-((?:[0-9]+)+)   ->    ((?:[0-9]+)+).((?:[0-9]+)+)or
10_May_1989 -> 10 May 1989
Code: [Select]
((?:[0-9]+)+)_((?:[a-zA-Z]+)+)_((?:[0-9]+)+) ->     ((?:[0-9]+)+) ((?:[a-zA-Z]+)+) ((?:[0-9]+)+)
Don't think you can do that... because the matching regex will be replaced with the replaceWith string.
You can not replace with another regex.

I tried: -
Code: [Select]
@var $arr = GetSourceSelectedPaths();
@var $items = arrayCount($arr);
@var $CurrentNameFullPath;
@var $OrgName;
@var $NewName;
@var $re = "((?:[0-9]+)+)-((?:[0-9]+)+)-((?:[0-9]+)+)";
@var $str = $OrgName;
@var $n = 0;

for( $n = 0; $n < $items; $n++ )
{
  $CurrentNameFullPath = $arr[ $n ];
  $OrgName = PathGetNamePart( $CurrentNameFullPath );
  $NewName = StrRegExpReplace($str, $re, ".");
 
  RenameFile( $arr, $NewName, "SILENT" );
  }

But I get - Error : -23 => Parameter wrong type or in the mass rename - 'invalid characters' msg
I had the regex's working  outside MC - but they don't work in the mass rename or the script (yet)   :-[
What row did it not like ?
Hmm should you not use $OrgName for StrRegExpReplace ?
And I guess the error is for RenameFile(..) You can still not send an entire array into that. first paramter should be a string that is the is full target path of the file.

if you get invalid char in multirename then you got an invalid char into the file name like :| or some other invalid character.


Ulfhednar

  • Contributor
  • Elite Member
  • *****
  • Posts: 445
    • View Profile
Re: Search routine
« Reply #32 on: August 18, 2013, 16:47:26 »
I added a couple of screen shots above to help explain what is happening with mass rename & the regex's.
Debug error was row 13 then 14 when I used $Orgname

(I'm no expert I only know enough to cause trouble!) Hungry:- + forces regex engine to keep looking for matches so
Code: [Select]
((?:[0-9]+)+)should find any combination of digits.  Useful for dates etc where you may have 1-12-1980
Code: [Select]
((?:[0-9]+)+)-((?:[0-9]+)+)-((?:[0-9]+)+)should match that sequence.
In your example you predefined the # of digits with [0-9] [0-9], if it works with + you can (in theory!) match more than the 2 digits without defining multiple [0-9]s. 

If I use $Orgname I think I drop part of the sequence which links to $newname, maybe I need to have 2 {....} operations defined, one to run the regex, then the next to take the result & use it to rename.  ???

My bad  - I wasn't thinking replace regex with regex but how the groups would appear as a regex (trying out variations in mass rename & confusing myself!)

Thanks for your time Mathias.

Ulfhednar

  • Contributor
  • Elite Member
  • *****
  • Posts: 445
    • View Profile
Re: Search routine
« Reply #33 on: August 18, 2013, 17:33:10 »
Code: [Select]
@var $arr = GetSourceSelectedPaths();
@var $items = arrayCount($arr);
@var $CurrentNameFullPath;
@var $OrgName;
@var $re = "((?:[0-9]+)+)-((?:[0-9]+)+)-((?:[0-9]+)+)";
@var $result;
@var $n = 0;

for( $n = 0; $n < $items; $n++ )
{
  $CurrentNameFullPath = $arr[ $n ];
  $OrgName = PathGetNamePart( $CurrentNameFullPath );
  $result = StrRegExpReplace($OrgName, $re, ".");
 
  RenameFile( $arr, $result, "SILENT" );
 
  }

Seems better - no errors but not renaming.  Will keep trying.

Update
Code: [Select]
@var $arr = GetSourceSelectedPaths();
@var $items = arrayCount($arr);
@var $CurrentNameFullPath;
@var $OrgName;
@var $re = ".A[0-9][0-9]B[0-9][0-9].";
@var $result;
@var $n = 0;

for( $n = 0; $n < $items; $n++ )
{
  $CurrentNameFullPath = $arr[ $n ];
  $OrgName = PathGetNamePart( $CurrentNameFullPath );
  $result = StrRegExpReplace($OrgName, $re, ".");
 
  RenameFile( $arr, $result, "SILENT" );
 
  }

changes
Code: [Select]
"This.String.A02B22.SUBString-ZZZZ" ->  "This.String.SUBString-ZZZZ"...so the regex is definitely not MC friendly....  :'(
I will need to find out how this system handles the 'hungry' (greedy) operation ::)

As it is the regex I have used would match all combos of 1-12-123; 123-1-12; 1234-12-123 etc so it would be handy to transpose this into the MC-friendly form.

OT -
what do I do to access multiple selections?  At the moment this will only do the first of any selected group.  I'm guessing it's related to $n.

Update 2

Code: [Select]
@var $arr = GetSourceSelectedPaths();
@var $items = arrayCount($arr);
@var $CurrentNameFullPath;
@var $OrgName;
@var $re = "\d+";
@var $result;
@var $n = 0;

for( $n = 0; $n < $items; $n++ )
{
  $CurrentNameFullPath = $arr[ $n ];
  $OrgName = PathGetNamePart( $CurrentNameFullPath );
  $result = StrRegExpReplace($OrgName, $re, "x");
 
  RenameFile( $arr, $result, "SILENT" );
}

Will change multiple digits but won't retain the initial sequence
1.23.234
becomes x.x.x

I want to pass the original string back modifying only certain characters. 
As in  eg -
Code: [Select]
((?:[0-9]+)+)_((?:[a-zA-Z]+)+)_((?:[0-9]+)+) ->     ((?:[0-9]+)+) ((?:[a-zA-Z]+)+)-((?:[0-9]+)+)
10_May_1980 -> 10 May-1980

I suspect I'm not going to be able to do this.    :(
« Last Edit: August 18, 2013, 19:06:03 by Ulfhednar »

Mathias (Author)

  • Administrator
  • Elite Member
  • *****
  • Posts: 3602
    • View Profile
    • Multi Commander
Re: Search routine
« Reply #34 on: August 18, 2013, 22:42:49 »
The rename will never work because you are still using $arr in rename file. That will not work.

RenameFile($arr, $result, "SILENT" );

RenameFile(...) does not accept an array.. it wants a string.

What you trying to do will never work, you can not insert a regex in the replace with part. There is no way for it to match stuff like that.
There no way the engine should understand how match that.

Maybe use the StrRegExFind and from that you get at what position the regex match start. and from there you can get the substring and rebuild the string and replace the part you want manually.
Or you can also loop the string and check character by character and if  you find a - or _ of after a number you replace it.
But then you are not using any cool regexp.

Regex engine in MC is following the regex standard. Think it is called ECMA-262 .. What I understand it is the same as perl uses.
MC does not use any special own regex. it is build into the C++ language as of the C++11 Standard.
But regex are written different depending on what you want to do. and there are 100 ways to do the same thing.

« Last Edit: August 18, 2013, 22:53:49 by Mathias (Author) »

Ulfhednar

  • Contributor
  • Elite Member
  • *****
  • Posts: 445
    • View Profile
Re: Search routine
« Reply #35 on: August 19, 2013, 00:01:05 »
Thanks Mathias.
I got confused with $arr - when it was present the script completed one pass, but not the rest of the selection.  (As you must realize).
So I left $arr in order to get the operation to complete & thus find out which part of the regex would work. 

I started using perl type commands but as you say the StrRegExpReplace function won't allow me to do a 'batch' of separate ops.

Took me a while to see which string was the right one with which to replace $arr.  But I now have a better array usage!
 
This will make changes to multiple selections now (not what I want of course but better from a script point of view.) -

Code: [Select]
@var $arr = GetSourceSelectedPaths();
@var $items = arrayCount($arr);
@var $CurrentNameFullPath;
@var $OrgName;
@var $re = "\w.";
@var $result;
@var $n = 0;

for( $n = 0; $n < $items; $n++ )
{
  $CurrentNameFullPath = $arr[ $n ];
  $OrgName = PathGetNamePart( $CurrentNameFullPath );
  $result = StrRegExpReplace($OrgName, $re, " ");
 
  RenameFile( $CurrentNameFullPath, $result, "SILENT" );
 
  }

I cannot use ( ) within the $re define multiple segments, soI think I would have to do a loop of repeat operations to change each element individually.  Quite frustrating when I can run the regex's I posted in a text editor on text & get the right result.   

Regular Expression Editor (RegExpEditor), amongst others, on sourceforge could be useful for regex types.


Well I guess that is enough headache for one day.   ;)

+++

Update
Thought I'd add the regex I was using to change the date field I mentioned above for clarity.  (I was showing the whole string as regex transpositions instead of in -> out)
10_May_1989 -> 10 May 1989
find -
Code: [Select]
((?:[0-9]+)+)_((?:[a-zA-Z]+)+)_((?:[0-9]+)+)replace -
Code: [Select]
\1 \2-\3
I think I can see a way for each \#<match> for be given its' own instruction.  More headaches req'd.

The main advance for me here is the visualization of matrices in your script.  I was thinking more DOS Basic & linear commands.  Matrix application is more dynamic of course.  So thanks for forcing me down that road.   ;D
« Last Edit: August 19, 2013, 09:28:03 by Ulfhednar »

Ulfhednar

  • Contributor
  • Elite Member
  • *****
  • Posts: 445
    • View Profile
Re: Search routine
« Reply #36 on: August 20, 2013, 23:18:39 »
Mathias - Thanks to your comment on C++ regex standards &
http://www.cplusplus.com/reference/regex/match_replace/

I was able to transpose my original regex into a compatible format, the $token & trial & error gave me my desired date mod
10_May_1876  ->  10.May-1876

That + your earlier help on the script writing gave me a handy button for renaming date named folders: -

Code: [Select]
@var $arr = GetSourceSelectedPaths();
@var $items = arrayCount($arr);
@var $CurrentNameFullPath;
@var $OrgName;
@var $re = "([0-9]+)_([a-zA-Z]+)_([0-9]+)";
@var $result;
@var $n = 0;

for( $n = 0; $n < $items; $n++ )
{
  $CurrentNameFullPath = $arr[ $n ];
  $OrgName = PathGetNamePart( $CurrentNameFullPath );
  $result = StrRegExpReplace($OrgName, $re, "$3.$2-$1");
 
  RenameFile( $CurrentNameFullPath, $result, "SILENT" );

}

Switching the match tokens around
Code: [Select]
$result = StrRegExpReplace($OrgName, $re, "$3.$2-$1");allowed
10_May_1876  ->  1876.May-10

Obviously this opens up a great deal of possibilities for the regex function in my script experiments.  (& anyone else who may need to use regexs....)


OT
FYI I don't seem to be able to save scripts in the debugger anymore - New / F1 don't work / greyed-out (3.3 build 1470)
« Last Edit: August 21, 2013, 00:04:20 by Ulfhednar »