C# replace with regular expression - c#

This code doesn't work, but it works with other expressions, like (?:[A-Za-z][A-Za-z0-9_]*).
The below expression works correctley in a regular expression tester, but it doesn't replace Hello with id in this code:
string test = "int Hello := 2 ;";
string pattern = "\b(?!int|bool)(?:[A-Za-z][A-Za-z0-9_]*)\b";
string replacement = "Id";
Regex rgx = new Regex(pattern);
string newline = rgx.Replace(test, replacement);

You should escape backslashes or use # beginning of your string and make it verbatim string. \b has a special meaning in C# which is backspace, see documentation: Escape Sequences
string pattern = #"\b(?!int|bool)(?:[A-Za-z][A-Za-z0-9_]*)\b";

Related

replace variable name in formula with Regex.Replace

In c#, I want use a regular expression to replace each variable #A with a number withouth replacing other similar variables like #AB
string input = "3*#A+3*#AB/#A";
string value = "5";
string pattern = "#A"; //<- this doesn't work
string result = Regex.Replace(input, pattern, value);
// espected result = "3*5+3*#AB/5"
any good idea?
Use a word boundary \b:
string pattern = #"#A\b";
See regex demo (Context tab)
Note the # before the string literal: I am using a verbatim string literal to declare the regex pattern so that I do not have to escape the \. Otherwise, it would look like string pattern = "#A\\b";.

How to replace two first characters before underscore with regex?

I have example this string:
HU_husnummer
HU_Adrs
How can I replace HU? with MI?
So it will be MI_husnummer and MI_Adrs.
I am not very good at regex but I would like to solve it with regex.
EDIT:
The sample code I have now and that still does not work is:
string test = Regex.Replace("[HU_husnummer] int NOT NULL","^HU","MI");
Judging by your comments, you actually need
string test = Regex.Replace("[HU_husnummer] int NOT NULL",#"^\[HU","[MI");
Have a look at the demo
In case your input string really starts with HU, remove the \[ from the regex pattern.
The regex is #"^\[HU" (note the verbatim string literal notation used for regex pattern):
^ - matches the start of string
\[ - matches a literal [ (since it is a special regex metacharacter denoting a beginning of a character class)
HU - matches HU literally.
String varString="HU_husnummer ";
varString=varString.Replace("HU_","MI_");
Links
https://msdn.microsoft.com/en-us/library/system.string.replace(v=vs.110).aspx
http://www.dotnetperls.com/replace
using Substring
var abc = "HU_husnummer";
var result = "MI" + abc.Substring(2);
Replace in Regex.
string result = Regex.Replace(abc, "^HU", "MI");

How to select a group of characters in a string using regular expression

I am new to regular expression. I am trying to match a group of characters using regular expression, but it does not work.
Here is my code.
string test = "Hello$#%$all";
string regex = "($#%$)";
string result = Regex.Replace(test, regex, "\n");
Any help??
You need to escape the characters that has special meaning in regular expressions.
string test = "Hello$#%$all";
string regex = #"\$#%\$";
string result = Regex.Replace(test, regex, "\n");
Characters like $ has special meaning when used in a regular expression. So tell apart whether it's a character used to signify something in the expression or whether you need to literally match that character, you can escape it using a \

C# Regular Expressions

I have a string that has multiple regular expression groups, and some parts of the string that aren't in the groups. I need to replace a character, in this case ^ only within the groups, but not in the parts of the string that aren't in a regex group.
Here's the input string:
STARTDONTREPLACEME^ENDDONTREPLACEME~STARTREPLACEME^ENDREPLACEME~STARTREPLACEME^BLAH^ENDREPLACEME~STARTDONTREPLACEME^BLAH^ENDDONTREPLACEME~
Here's what the output string should look like:
STARTDONTREPLACEME^ENDDONTREPLACEME~STARTREPLACEMEENDREPLACEME~STARTREPLACEMEBLAHENDREPLACEME~STARTDONTREPLACEME^BLAH^ENDDONTREPLACEME~
I need to do it using C# and can use regular expressions.
I can match the string into groups of those that should and shouldn't be replaced, but am struggling on how to return the final output string.
I'm not sure I get exactly what you're having trouble with, but it didn't take long to come up with this result:
string strRegex = #"STARTREPLACEME(.+)ENDREPLACEME";
RegexOptions myRegexOptions = RegexOptions.None;
Regex myRegex = new Regex(strRegex, myRegexOptions);
string strTargetString = #"STARTDONTREPLACEME^ENDDONTREPLACEME~STARTREPLACEME^ENDREPLACEME~STARTREPLACEME^BLAH^ENDREPLACEME~STARTDONTREPLACEME^BLAH^ENDDONTREPLACEME~";
string strReplace = "STARTREPLACEMEENDREPLACEME";
return myRegex.Replace(strTargetString, strReplace);
By using my favorite online Regex tool: http://regexhero.net/tester/
Is that helpful?
Regex rgx = new Regex(
#"\^(?=(?>(?:(?!(?:START|END)(?:DONT)?REPLACEME).)*)ENDREPLACEME)");
string s1 = rgx.Replace(s0, String.Empty);
Explanation: Each time a ^ is found, the lookahead scans ahead for an ending delimiter (ENDREPLACEME). If it finds one without seeing any of the other delimiters first, the match must have occurred inside a REPLACEME group. If the lookahead reports failure, it indicates that the ^ was found either between groups or within a DONTREPLACEME group.
Because lookaheads are zero-width assertions, only the ^ will actually be consumed in the event of a successful match.
Be aware that this will only work if delimiters are always properly balanced and groups are never nested within other groups.
If you are able to separate into groups that should be replaced and those that shouldn't, then instead of providing a single replacement string, you should be able to use a MatchEvaluator (a delegate that takes a Match and returns a string) to make the decision of which case it is currently dealing with and return the replacement string for that group alone.
You may also use an additional regex inside the MatchEvaluator. This solution produces the expected output:
Regex outer = new Regex(#"STARTREPLACEME.+ENDREPLACEME", RegexOptions.Compiled);
Regex inner = new Regex(#"\^", RegexOptions.Compiled);
string replaced = outer.Replace(start, m =>
{
return inner.Replace(m.Value, String.Empty);
});

Using \b in C# regular expressions doesn't work?

I am wondering why the following regex does not match.
string query = "\"1 2\" 3";
string pattern = string.Format(#"\b{0}\b", Regex.Escape("\"1 2\""));
string repl = Regex.Replace(query, pattern, "", RegexOptions.CultureInvariant);
Note that if I remove the word boundary characters (\b) from pattern, it matches fine. Is there something about '\b' that might be tripping this up?
A quote is not a word character, so \b will not be a match if it is there. There is no word character before the quote; so, before the quote, there is no transition between word characters and non-word characters. So, no match.
From your comment you are trying to remove word characters from a string. The most straightforward way to do that would be to replace \w with an empty string:
string repl = Regex.Replace(query, "\w", "", RegexOptions.CultureInvariant);
you are expecting a whitespace.
it isn't finding one.
replace
string query = "\"1 2\" 3";
with
string query = "\" 1 2 \" 3";
and you'll see what i mean.

Categories

Resources