Remove text from string with JScript - replace

I have a JScript which iterates through selected elements in the project browser. What I would like to do is to remove a part of the name string.
Example name:
"Random name (other text)"
I would like the script to change the name to "Random name". For this example I always want to remove the brackets and the text inside the brackets.
How do I do this with JScript? Replace?

The function split solved my issue:
oldName = e.Name;
newName = oldName.split(" (");
e.Name = newName[0];

Related

Refactoring - Replace all Fieldnames Starting with "_"

I'm about to refactor my project and I'd like to replace all variable names that start with "_" e.g. private final String _name; -> private final String name;
My Template fo FIND the Variables is simply:
$FieldName$
I set this regex for the variable name:
[_][a-z]+
Well, But this will just return a list of my variables starting with "_", how do I strip the _ and then set the new variable name?
EDIT: I edited this topic so maybe Eclipse users can tell me how to solve this with Eclipse.
The following works for me when using IntelliJ IDEA's Structural Search and Replace
Using your Search template, use the following Replacement template:
$NewName$
With Script text:
// FieldName refers to the Search template variable
if (FieldName instanceof com.intellij.psi.PsiVariable) {
com.intellij.psi.PsiVariable var = (com.intellij.psi.PsiVariable) FieldName;
var.getName().substring(1);
} else {
String string = FieldName.getText();
int index = string.indexOf('_');
string.substring(0, index) + string.substring(index + 1);
}
You can do this on a text basis via regular expressions in IntelliJ
Hit ctrl-shift-r to open "Replace in Path". Ensure Regular Expression is ticked, and enter the following:
Text to find: ([_])([a-zA-Z]+)
Replace with: $2
Beware, a possible issue here is that other text strings (e.g. EXIT_ON_CLOSE) might also be picked up by the regular expression, and you might have to be careful not to apply the replace in those cases (or adjust your regular expression to be smarter).

How to replace "&A-Z" with the orinal text minus the &

I'm looking for a way to replace "&" from a text string when it's followed by text. For example;
Input Text: "Ne&w && Edit Record"
Required Output: New & Edit Record"
The reason for "Ne&w" is that "&" then shows w as the shortcut key (underscored in the UI) and the reason for the "&&" is so that a single "&" is displayed in the text.
When using the input text as the value for the text propery on a command button the text displays as expected, but when I pass the text property to a message box it displays the input text in the message box dialog.
I don't want to use the Tag property to store the message I want in the message box as I use the tag property for another value.
Try this:
Dim input As String = "Ne&w && Edit Record"
Dim output As String = "New & Edit Record"
Dim p As String = Regex.Replace(input, "&(?<first>\w|&)", "${first}")
MessageBox.Show(output = p) 'shows True
In the above Regex expression I am capturing an ampersand followed by either a letter or another ampersand, and replacing that sequence with a symbol coming after the ampersand. <first> is a named group, it is used for Regex replacement.
See Regex.Replace on MSDN.
You can remove all & signs not followed by a & sign. Match them like this:
&(?!&)
See it in action

Find & Replace with a wildcard in Xcode 4

I'm trying to find all instances of text inside "" marks with a semi-colon directly after them, and replace the text inside the "" marks. So, for example:
"FirstKey" = "First value";
"SecondKey" = "Second value";
"ThirdKey" = "Third value";
Would find only those values after the equals signs, and could replace them all (with a single string) at once, like so:
"FirstKey" = "BLAH";
"SecondKey" = "BLAH";
"ThirdKey" = "BLAH";
How can I do this? I found some stuff referring to regular expressions in Xcode 3, but such functionality seems either gone or hidden in Xcode 4.
Regular expression replace is still available in Xcode 4. Use "Replace" and set style to "Regular Expression", use "([^"]*)"; as pattern and replace with "BLAH";.

Parse string where "separator" can be part of data?

I have std strings like these:
UserName: Message
At first look it seems like an easy problem, but this issue is in that the name's last character could be a ':' and the first letter of the message part of the string could be a ':' too. The user could also have spaces in their name.
So A user might be names 'some name: '
and might type a message ' : Hello'
Which would look like:
'some name: : : Hello'
I do have the list (vector) of usernames though.
Given this, is there a way I could extract the username from this sort of string? (Ideally without having to iterate through the list of users)
Thanks
Try a regex like (\w+?):\ \w+.
If you can't gaurentee that the username won't contain a ":" characters, and you want to avoid iterating the entire list each time to check, you could try a shortcut.
Keep a vector of just the usernames that contain special chars (I'm imagining that this is a small subset of all usernames). Check those first, if you find a match, take the string after [username]: . Otherwise, you can simply do a naive split on the colon.
I would use string tokens
string text = "token, test string";
char_separator<char> sep(":");
tokenizer< char_separator<char> > tokens(text, sep);
BOOST_FOREACH(string t, tokens)
{
cout << t << "." << endl;
}
The way I would approach this is to simply find the first colon. Split the string there, and then trim the two remaining strings.
It's not entirely clear to me why there are additional colons and if they are part of the value. If they need to be removed, then you'll also need to strip them out.

search/replace boost regex C++

I am having a regex replace issue that i can't seem to figure out for replacing some configured parameter for a file path.
Here is what I have so far:
The regex for a filepath may not be perfect but it seems to work ok.
regex: ^(?<path>[^\\/*?<>|]+)\\\\(?<filename>.+)\\.(?<ext>.mp4$)
file name match results name: $2
So what this is doing is searching a listing of files where the extension is mp4 and using the configured match result, it will return that as a "file name".
Target string examples,
\\\\folder\music\hello.mp4
result filename = "hello"
What I would like to do is be able to take either the results from a regex match and be able to replace the name of the file/extension/path by a configured setting.
So If someone wanted for all the matched results to replace the file name with "goodbye", how would i accomplish this. This is what i have now.
std::string sz_regex_pattern("^(?<path>[^\/*?<>|]+)\\(?<filename>.+)\.(?<ext>.mp4$)");
boost::cmatch rm;
boost::regex pattern(sz_regex_pattern, regex::icase|regex_constants::perl);
std::string complete_file_name_path = "\\folder\music\hello.mp4";
bool result = boost::regex_match(complete_file_name_path , rm, pattern);
std::string old_filename= rm.format("$2"); // returns the name of the file only
What appears to work but limits it to a filename where the folder is not the same name so,
\\folder\music\hello\hello.mp4 would have issues with the regex_replace below.
std::string new_filename = "goodbye";
std::string sz_new_file_name_path = boost::regex_replace(complete_file_name_path, old_filename, new_filename);
so i can later,
boost::filesystem::rename(complete_file_name_path, sz_new_file_name_path);
Any help would be appreciated.
Find and replace is completely unnecessary because you already have all of the components you need to build the new path.
REPLACE
std::string sz_new_file_name_path = boost::regex_replace(complete_file_name_path, old_filename, new_filename);
WITH
// path + newFileName + ext
std::string sz_new_file_name_path = rm.format("$1") + "\\" + new_filename + "." + rm.format("$3")
You could probably split out the components to see what you have with:
^(.*?)\\?([^\\]+)\.([a-zA-Z0-9]+)$
edit or even less specific ^(.*?)\\?([^\\]+)\.([^.]+)$ non-validating
$1 = path
$2 = filename
$3 = extension
The separator between path, filename and extension are not captured.
With this information you could construct your own new string.
If you want to specifically search for say mp4's something like this would work:
^(.*?)\\?([^\\]+)\.mp4$