Here is an answer using regex as the OP asked.
To use regex, put the replacment text in a match ( ) and then replace that match with nothing string.Empty:
string text = @"werfds_tyer.abc.zip.ytu_20111223170226_20111222.20111222";
string pattern = @"(\.zip\.ytu)";
Console.WriteLine( Regex.Replace(text, pattern, string.Empty ));
// Outputs
// werfds_tyer.abc_20111223170226_20111222.20111222
Answer from ΩmegaMan on Stack OverflowHere is an answer using regex as the OP asked.
To use regex, put the replacment text in a match ( ) and then replace that match with nothing string.Empty:
string text = @"werfds_tyer.abc.zip.ytu_20111223170226_20111222.20111222";
string pattern = @"(\.zip\.ytu)";
Console.WriteLine( Regex.Replace(text, pattern, string.Empty ));
// Outputs
// werfds_tyer.abc_20111223170226_20111222.20111222
Just use String.Replace()
String.Replace(".zip.ytu", "");
You don't need regex for exact matches.
Using regex to remove non-alphabetical characters in a string.
regex - Remove a leading string using regular expression - Stack Overflow
How to remove a string of text with Regex tool?
Replace or remove a portion of string with a regex
Hi, I am trying to remove non-alphabetical characters from the following string.
let string = "Anne, I vote more cars race Rome-to-Vienna"
I can get the result I am looking for with the following...
let newString = string.toLowerCase().split('').filter(el => el !== ' ' && el !== '-' && el !== ',').join('')
However I would prefer to use a much shorter piece of code using regex. I have gone over a few regex examples and tutorials but it still kind of confused me. Does anybody know the simplest way to remove the unwanted characters from the above string?
Thanks!!!
What have you tried by yourself? What you want to achieve is not difficult, try e.g. this tutorial on Regular-Expresions.info.
You are thinking much to complicated. Try this:
^(The|An|A)\s+
and replace with the empty string.
See it here on Regexr
^ matches the start of the string.
(The|An|A) An alternation, matches the first fitting alternative.
\s+ matches at least one following whitespace.
Changes
The quick brown fox
A quick brown fox
In A sunny day
American An bank
To
quick brown fox
quick brown fox
In A sunny day
American An bank
Below is the complete one-line in perl:
perl -e 'my $a = "The quick brown fox jumps over the lazy dog"; $a =~ s/^\s*(?:The|An|A)\s+//gi; print $a;'
The part that does the replace is:
$a =~ s/^\s*(?:The|An|A)\s+//gi;
The regex that matches your words and spaces is /^\s*(?:The|An|A)\s+/
I'm trying to remove a word from a string along with anything that goes after it so that the string doesn't have double spaces and/or random symbols in place of the removed word. Can I do this with regex in C#?
I haven't found a way online to use a variable pattern and do regex formatting on it to get the symbol that follows it, whitespace or whatever it might be. Doesn't help that I don't understand the Regex regular expressions.
Still learning C#/programming so I have no clue how to achieve this, this is what I wrote if it were the same syntax as Console.WriteLine():
/** Removes given word from line
@ param line – original text line (string)
@ param remove – word to remove */
public static string RemoveWords (string line, string remove)
{
string punctuation = " [\\s,.;:!?()\\-]+";
string pattern = ("{0}{1}", remove, punctuation); // the pattern that I have no idea on how to format
Regex rgx = new Regex(pattern);
string newLine = rgx.Replace(line, string.Empty);
return newLine;
}Would really appreciate any help!
Edit: Solved, syntax I ended up needing was Regex.Escape():
/** Removes given word from line
@ param line – original line
@ param remove – word to remove */
public static string RemoveWords(string line, string remove)
{
string newLine = Regex.Replace(line, @"\b" + Regex.Escape(remove) + @"\b[^a-zA-z]", "");
newLine = Regex.Replace(newLine, @"\b" + Regex.Escape(remove) + @"\b", "");
return newLine;
}
How about just doing:
Regex rgx = new Regex("-L$");
string result = rgx.Replace("ABCD-L", "");
So basically: if the string ends with -L, replace that part with an empty string.
If you want to not only invoke the replacement at the end of the string, but also at the end of a word, you can add an additional switch to detect word boundaries (\b) in addition to the end of the string:
Regex rgx = new Regex("-L(\b|$)");
string result = rgx.Replace("ABCD-L ABCD ABCD-L", "");
Note that detecting word boundaries can be a little ambiguous. See here for a list of characters that are considered to be word characters in C#.
You also can use String.Replace() method to find a specific string inside a string and replace it with another string in this case with an empty string.
http://msdn.microsoft.com/en-us/library/fk49wtc1(v=vs.110).aspx
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);