I have a string containing mathematical expression like:
strExpression= a+b+tan(a)+tan(b)+a+b
I want to replace this expression with values of a and b(say a=10,b=20) so that it become:
10+20+tan(10)+tan(20)+10+20
But when I use Regex.Replace I get:
10+20+t10n(10)+t10n(20)+10+20
How can I replace the values of a and b at correct places.
I have filterd out MatchCollection object that contains:
{a},{b},{tan},{a},{tan},{b},{a},{b}
You can use word boundary \b before and after the variable name (e.g. \ba\b for variable a and \bb\b for variable b) to match the variable in the expression.
Use boundary \b:
\b The match must occur on a boundary
between a \w (alphanumeric) and a \W (nonalphanumeric) character.
for example:
\b\w+\s\w+\b
result is
"them theme" "them them" in "them theme them them"
so that use:
Regex.Replace(inputString, #"(\bb\b)", "20");
Regex.Replace(inputString, #"(\ba\b)", "10");
string expression = "strExpression= a+b+tan(a)+tan(b)+a+b";
expression = Regex.Replace(expression, #"\ba\b", "10");
expression = Regex.Replace(expression, #"\bb\b", "20");
Console.WriteLine(expression);
Try to be more specific in your regex rather than just replacing the values. Here are some rules which describes whether a character captured is variable or not
Variable must have binary operator(+,-,*,/) and optinally spaces to its right(if start) ,to its left(if end) or on both side. It can also have paranethis around it if passed in function.
So create a regex which satisfies these all conditions
You can do with this:
Regex Example
And then use the same way for b with replacement of 20
Related
I am trying to make a regular expression that will tell me if a string has {0#} where zero can be repeated. Once I confirm that a string has this I am then trying to set it to a variable so I can count the number of 0s and replace the # with another number. I have /([{0]})([#}])/g which works on detection but not on pulling it out to another variable.
Edit:
Thanks to all, the answer was
Regex regex = new Regex(#"\{(0+)(#)\}");
Match match = regex.Match(text);
if (match.Success)
{
int zeros = Regex.Matches(match.Value, "0").Count;
}
Use this:
\{(0+)(#)\}
character {
then one or more occurance of 0
a # sign
character }
Live Demo
You are super close. The problem you are having is because your capture group - the ( ) needs to be just around the zeroes. You also don't strictly need the other capture group unless you are doing something with it. You can rewrite your regex like this:
{(0+)#}
{ - match '{'
(0+) - match and capture one or more '0'
# - match '#'
} - match '}'
I have a regex that I am trying to pass a variable to:
int i = 0;
Match match = Regex.Match(strFile, "(^.{i})|(godness\\w+)(?<=\\2(\\d+).*?\\2)(\\d+)");
I'd like the regex engine to parse {i} as the number that the i variable holds.
The way I am doing that does not work as I get no matches when the text contains matching substrings.
It is not clear what strings you want to match with your regex, but if you need to use a vriable in the pattern, you can easily use string interpolation inside a verbatim string literal. Verbatim string literals are preferred when declaring regex patterns in order to avoid overescaping.
Since string interpolation was introduced in C#6.0 only, you can use string.Format:
string.Format(#"(^.{{{0}}})|(godness\w+)(?<=\2(\d+).*?\2)(\d+)", i)
Else, beginning with C#6.0, this seems a better alternative:
int i = 0;
Match match = Regex.Match(strFile, $#"(^.{{{i}}})|(godness\w+)(?<=\2(\d+).*?\2)(\d+)");
The regex pattern will look like
(^.{0})|(godness\w+)(?<=\2(\d+).*?\2)(\d+)
^^^
You may try this Concept, where you may use i as parameter and put any value of i.
int i = 0;
string Value =string.Format("(^.{0})|(godness\\w+)(?<=\\2(\\d+).*?\\2)(\\d+)",i);
Match match = Regex.Match(strFile, Value);
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");
I want to validate input string such that
5.0 is correct
5.5% is correct
So I started with the following code:
string decimalstring1 = "10000.55";
string decimalstring2 = "5%5%";
string expression = #"^\d|\d%";
Regex objNotNumberPattern = new Regex(expression);
Console.WriteLine(objNotNumberPattern.IsMatch(decimalstring1));
Console.WriteLine(objNotNumberPattern.IsMatch(decimalstring2));
Console.ReadLine();
But the problem is that with input like 5%5% it gives correct
How can I modify this expression to make this work?
string[] inputs = new string[] {
"1000.55",
"1000.65%",
"100",
"100%",
"1400%",
"5.5",
"5.5%",
"x",
".%"
};
string expression = #"^\d+[.]?\d*%?$";
Regex objNotNumberPattern = new Regex(expression);
foreach (var item in inputs)
Console.WriteLine(objNotNumberPattern.IsMatch(item));
UPDATE
string expression = #"^(\d+|\d+[.]\d+)%?$";
You get partial matches, because your expression does not anchor both sides. Your regex anchors the beginning, but not the end of the match.
Moreover, the placement of the left anchor ^ is incorrect, because it applies only to the left sub-expression
Adding a $ at the end should help:
^(\d|\d%)$
However, this is suboptimal: since the prefix of both expressions is the same, and they differ by an optional suffix %, you could use %? to simplify the expression:
^\d+%?$
This is better, but it would not match decimal point. To add this capability, change the expression as follows:
^(\d+|\d*[.]\d+)%?$
You're expression is the following: match when you find either of the following: a single digit at the start of the input string, or a single digit anywhere, followed by %. Probably not what you intended. I'd try something like this:
var expression = #"^\d+(\.\d+)?%?$";
This would translate to: match a positive number of digits at the start of the string, optionally followed by a dot and any number of digits (fractional part), optionally ending with a % sign.
You could try this ^\d+([.]\d+)?%?$ it works with: (tested)
1000.55
1000.65%
100
100%
1400%
5.5
5.5%
Hope it helps!
i think this is the best one :
^\d{0,2}(\.\d{1,4})? *%?$
source : Here
this worked for me:
/(^100([.]0{1,2})?)$|(^\d{1,2}([.]\d{0,2})?)$/
For only two decimals and no more than 100 [0.00 - 100.00]
I got a problem about string replacement, because of substrings chancing somewhere. For example
component1 = 5;
component2 = 6;
component10= 7;
when I want to replace component1 with variable, component10 will change as variable0
How should I prevent this in C#
You can use non word boundary.So,your regex would be
\bcomponent1\b
This would match component1 as a separate word and not as a substring
your code would be
string output=Regex.Replace(input,#"\bcomponent1\b");
# is required else \b would be treated as special character which would give you error because \b is not a valid escape character or use \\b
Just replace them in descending order of substring length.