Useful string expressions (mel)

Collection of useful string expressions for mel

 The nice name of an object from the ‘long’ path

string $matchString = "|locator1|null1|pCube1";
match("[^|]*$", $matchString);

// Result: pCube1 //

The parent of an object from the ‘long’ path

string $matchString = "|locator1|null1|pCube1";
substitute("|[^|]*$", $matchString, "");

// Result: |locator1|null1 //

Match the specified attribute name of an object

string $matchString = "|locator1|null1|pCube1.translate";
match("[^.]*$", $matchString);

// Result: translate //
string $matchString = "|locator1|null1|pCube1.vtx[0]";
match("[^.]*$", $matchString);

// Result: vtx[0] //

Match the nice name of an object excluding any includeded attributes

string $matchString = "|locator1|null1|pCube1.translate";
match("[^|]*$", match("^[^.]*", $matchString));

// Result: pCube1 //

Match the attribute excluding any array index information

string $matchString = "|locator1|null1|pCube1.vtx[0]";
match("[^.]*$", match("^[^[]*", $matchString));

// Result: vtx //

Remove trailing character (i.e Whitespace, new line, tab etc)

string $matchString = "text string nrt";
substitute("[( ntr)]*$", $matchString, "");

// Result: text string //

Remove leading character (i.e Whitespace, new line, tab etc)

string $matchString = " nrttext string";
substitute("^[( ntr)]*", $matchString, "");

// Result: text string //

Drive letter from path

string $matchString = "c:\testdir\subdirectoryA/subdirectoryB/yourFile.txt";
match("^[^:]*", $matchString);

// Result: c //

Full directory from path

string $matchString = "c:\testdir\subdirectoryA/subdirectoryB/yourFile.txt";
substitute("[^(\/)]*$", $matchString, "");

// Result: c:testdirsubdirectoryA/subdirectoryB/ //

Full filename from path

string $matchString = "c:\testdir\subdirectoryA/subdirectoryB/yourFile.txt";
match("[^(\/)]*$", $matchString);

// Result: yourFile.txt //

File extension from path

string $matchString = "c:\testdir\subdirectoryA/subdirectoryB/yourFile.txt";
match("[^(.)]*$", $matchString);

// Result: txt //

Filename excluding extension from path

string $matchString = "c:\testdir\subdirectoryA/subdirectoryB/yourFile.txt";
match("^[^.]*", match("[^(\/)]*$", $matchString));;

// Result: yourFile //

Backslashes to forward slashes

string $matchString = "c:\testdir\subdirectoryA/subdirectoryB/yourFile.txt";
substituteAllString($matchString, "\", "/");

// Result: c:/testdir/subdirectoryA/subdirectoryB/yourFile.txt //

 

Comments

4 responses to “Useful string expressions (mel)”

  1. Mitch Avatar

    Thanks for all your Mel stuff.
    Very useful.

  2. nunzio Avatar
    nunzio

    great!! 😀 thanks a lot

  3. lito Avatar

    Great suggestions, thank you!
    Here's one that bugs me.
    What's the match to get this out of the string below:

    string $matchString = "|locator1|null1|pCube1";

    // Result: locator1|null1|pCube1

  4. lito Avatar

    nm.. brain fart..
    mystring = "|path1|path2|path3.vtx[0]"
    mystring[0:]

Leave a Reply

Your email address will not be published. Required fields are marked *