Three moments:
- join array items with
|without side spaces - enclose regex alternation group into parentheses
(...|...) - specify word boundary
\bto match a separate words
var removeUselessWords = function(txt) {
var uselessWordsArray =
[
"a", "at", "be", "can", "cant", "could", "couldnt",
"do", "does", "how", "i", "in", "is", "many", "much", "of",
"on", "or", "should", "shouldnt", "so", "such", "the",
"them", "they", "to", "us", "we", "what", "who", "why",
"with", "wont", "would", "wouldnt", "you"
];
var expStr = uselessWordsArray.join("|");
return txt.replace(new RegExp('\\b(' + expStr + ')\\b', 'gi'), ' ')
.replace(/\s{2,}/g, ' ');
}
var str = "The person is going on a walk in the park. The person told us to do what we need to do in the park";
console.log(removeUselessWords(str));
Answer from RomanPerekhrest on Stack OverflowThree moments:
- join array items with
|without side spaces - enclose regex alternation group into parentheses
(...|...) - specify word boundary
\bto match a separate words
var removeUselessWords = function(txt) {
var uselessWordsArray =
[
"a", "at", "be", "can", "cant", "could", "couldnt",
"do", "does", "how", "i", "in", "is", "many", "much", "of",
"on", "or", "should", "shouldnt", "so", "such", "the",
"them", "they", "to", "us", "we", "what", "who", "why",
"with", "wont", "would", "wouldnt", "you"
];
var expStr = uselessWordsArray.join("|");
return txt.replace(new RegExp('\\b(' + expStr + ')\\b', 'gi'), ' ')
.replace(/\s{2,}/g, ' ');
}
var str = "The person is going on a walk in the park. The person told us to do what we need to do in the park";
console.log(removeUselessWords(str));
May be this is what you want:
//Remove all instances of the words in the array
var removeUselessWords = function(txt) {
var uselessWordsArray =
[
"a", "at", "be", "can", "cant", "could", "couldnt",
"do", "does", "how", "i", "in", "is", "many", "much", "of",
"on", "or", "should", "shouldnt", "so", "such", "the",
"them", "they", "to", "us", "we", "what", "who", "why",
"with", "wont", "would", "wouldnt", "you"
];
var expStr = uselessWordsArray.join("\\b|\\b");
return txt.replace(new RegExp(expStr, 'gi'), '').trim().replace(/ +/g, ' ');
}
var str = "The person is going on a walk in the park. The person told us to do what we need to do in the park";
console.log(removeUselessWords(str));
//The result should be: "person going walk park. person told need park."
Remove multiple words from a string
Regular Expressions - excluding multiple words with regexr - Statalist
A regex method to remove a line with a certain word?
You say 'comments', so I assume you are using the bulk edit metadata search tab, searching the comments field - correct? If yes, be very careful, this is so powerful it can ruin your library - work on a copy. (If you're not in the bulk edit metadata, but in the Editor, the following should still work.)(And if you are in the main Editor, be careful to limit this to the current file, or marked text, or it will do the entire book.)
That gets tricky, depending on what you mean by a 'line'. A sentence, perhaps defined by 'spaceCapital then some words then Periodspace' ? For that you might try something like:
" [A-Z].*?yourword.*?\. " - without the quotes, they just show the space characters at the ends. Make sure case sensitive is checked. Replace with nothing. (Try with and without the first space - if your sentence starts a paragraph it will have no leading space.)
If a 'line' is actually a paragraph, try a similar thing only with <p>.*?yourword.*?</p> (And if the paragraphs have some class selector you have to include that, like <p class="something">. Look at the html source tab of the comments to see what you actually have.) This might remove several sentences.
But you also say "using the name they start with" - meaning the first word? Yet your example is not that way. If it is indeed the first word, change the string to something like "yourword.*?\. " or <p>yourword.*?</p>
And because this sort of search is VERY wide open, don't do a "replace all" until you are really sure it does what you want and nothing more. Much safer to step through one at a time.
More on reddit.comawk - Remove multiple strings from file on command line, high performance - Unix & Linux Stack Exchange
I use
bannedWord = ['Good','Bad','Ugly']
toPrint = 'Hello Ugly Guy, Good To See You.'
print(' '.join(i for i in toPrint.split() if i not in bannedWord))
Here's a solution with regex:
import re
def RemoveBannedWords(toPrint,database):
statement = toPrint
pattern = re.compile("\\b(Good|Bad|Ugly)\\W", re.I)
return pattern.sub("", toPrint)
toPrint = "Hello Ugly Guy, Good To See You."
print(RemoveBannedWords(toPrint,bannedWord))
As the title says. I have a file that contains way too many comments and I want to remove them using the name they start with. So basically I'm looking for regex or regex function that allows searching for a word then after finding that word it deletes the whole line it is in. For example: 1 Words words words 2 Words test words If I look for the word "test," it would delete the entire line 2, "Words test words". Anyone know a way to do this?
You say 'comments', so I assume you are using the bulk edit metadata search tab, searching the comments field - correct? If yes, be very careful, this is so powerful it can ruin your library - work on a copy. (If you're not in the bulk edit metadata, but in the Editor, the following should still work.)(And if you are in the main Editor, be careful to limit this to the current file, or marked text, or it will do the entire book.)
That gets tricky, depending on what you mean by a 'line'. A sentence, perhaps defined by 'spaceCapital then some words then Periodspace' ? For that you might try something like:
" [A-Z].*?yourword.*?\. " - without the quotes, they just show the space characters at the ends. Make sure case sensitive is checked. Replace with nothing. (Try with and without the first space - if your sentence starts a paragraph it will have no leading space.)
If a 'line' is actually a paragraph, try a similar thing only with <p>.*?yourword.*?</p> (And if the paragraphs have some class selector you have to include that, like <p class="something">. Look at the html source tab of the comments to see what you actually have.) This might remove several sentences.
But you also say "using the name they start with" - meaning the first word? Yet your example is not that way. If it is indeed the first word, change the string to something like "yourword.*?\. " or <p>yourword.*?</p>
And because this sort of search is VERY wide open, don't do a "replace all" until you are really sure it does what you want and nothing more. Much safer to step through one at a time.
AvidEReader has some excellent points and if you're using the Calibre editor to edit an ePub, there is probably no better advice.
However, you did say "file" and gave an example "line", so perhaps you're looking at lines in a .txt file that is not inside an epub (and therefore compressed), and not embedded in html paragraphs. If that's the case, I'd recommend the sed command. It's a command-line stream editor that works line-by-line on a text file.
The command sed '/test/d' a-copy-of-your-data-file.txt would do what you say you want.
-
the command passed to sed is enclosed in single quotes
-
"test" is the text you want to match
-
"/" and "/" set off the data from the command that will work on that data
-
"d" means "delete this line".
-
-
"a-copy-of-your-data-file.txt" is a copy of your file. sed will work on any ascii file -- it does not need a suffix -- please do use a copy, not your original file: sed's work is not reversible. It is only too easy to make a typo and there's no going back.
Oh yes -- I didn't ask which OS you use, but sed is available in linux and macOS, and Windows 10 via Windows Subsystem for Linux, so it's probably waiting for you now. Here's a link to the sed manual:
Good luck and take care.
Basically your regex should be like this
string input = @"1H|\^&|||ARCHITECT^8.10^F3453010030^H1P1O1R1C1Q1L1|||Admin||||P|1|20150511083525
1D";
string pattern = @"([^\w]*Admin[^\w]*)+|[|\\^&\r\n]+";
string replacement = "|";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, replacement);
Not necessary to use regex here. You can use Replace function;
yourString.Replace("Admin|", string.Empty);
If using Regex is important for you - try this variant:
Regex r = new Regex(@"\bAdmin|\b");
var output = r.Replace(s, string.Empty);
