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 //

 

4 thoughts on “Useful string expressions (mel)”

  1. 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

Leave a Reply to lito Cancel reply

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