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);
Related
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.
How to match all first digits before # in this line
26909578#Sbrntrl_7x06-lilla.avi#356028416#2012-10-24 09:06#0#http://bitshare.com/files/dvk9o1oz/Sbrntrl_7x06-lilla.avi.html#[URL=http://bitshare.com/files/dvk9o1oz/Sbrntrl_7x06-lilla.avi.html]http://bitshare.com/files/dvk9o1oz/Sbrntrl_7x06-lilla.avi.html[/URL]#http://bitshare.com/files/dvk9o1oz/Sbrntrl_7x06-lilla.avi.html#http://bitshare.com/?f=dvk9o1oz#http://bitshare.com/delete/dvk9o1oz/4511e6f3612961f961a761adcb7e40a0/Sbrntrl_7x06-lilla.avi.html
Im trying to get this number 26909578
My try
string text = #"26909578#Sbrntrl_7x06-lilla.avi#356028416#2012-10-24 09:06#0#http://bitshare.com/files/dvk9o1oz/Sbrntrl_7x06-lilla.avi.html#[URL=http://bitshare.com/files/dvk9o1oz/Sbrntrl_7x06-lilla.avi.html]http://bitshare.com/files/dvk9o1oz/Sbrntrl_7x06-lilla.avi.html[/URL]#http://bitshare.com/files/dvk9o1oz/Sbrntrl_7x06-lilla.avi.html#http://bitshare.com/?f=dvk9o1oz#http://bitshare.com/delete/dvk9o1oz/4511e6f3612961f961a761adcb7e40a0/Sbrntrl_7x06-lilla.avi.html";
MatchCollection m1 = Regex.Matches(text, #"(.+?)#", RegexOptions.Singleline);
but then its outputs all text
Make it explicit that it has to start at the beginning of the string:
#"^(.+?)#"
Alternatively, if you know that this will always be a number, restrict the possible characters to digits:
#"^\d+"
Alternatively use the function Match instead of Matches. Matches explicitly says, "give me all the matches", while Match will only return the first one.
Or, in a trivial case like this, you might also consider a non-RegEx approach. The IndexOf() method will locate the '#' and you could easily strip off what came before.
I even wrote a sscanf() replacement for C#, which you can see in my article A sscanf() Replacement for .NET.
If you dont want to/dont like to use regex, use a string builder and just loop until you hit the #.
so like this
StringBuilder sb = new StringBuilder();
string yourdata = "yourdata";
int i = 0;
while(yourdata[i]!='#')
{
sb.Append(yourdata[i]);
i++;
}
//when you get to that # your stringbuilder will have the number you want in it so return it with .toString();
string answer = sb.toString();
The entire string (except the final url) is composed of segments that can be matched by (.+?)#, so you will get several matches. Retrieve only the first match from the collection returned by matching .+?(?=#)
I have a fairly long string that contains sub strings with the following format:
project[1]/someword[1]
project[1]/someotherword[1]
There will be about 10 or so instances of this pattern in the string.
What I want to do is to be able to replace the second integer in square brackets with a different one. So the string would look like this for instance:
project[1]/someword[2]
project[1]/someotherword[2]
I''m thinking that regular expressions are what I need here. I came up with the regex:
project\[1\]/.*\[([0-9])\]
Which should capture the group [0-9] so I can replace it with something else. I'm looking at MSDN Regex.Replace() but I'm not seeing how to replace part of a string that is captured with a value of your choosing. Any advice on how to accomplish this would be appreciated. Thanks much.
*Edit: * After working with #Tharwen some I have changed my approach a bit. Here is the new code I am working with:
String yourString = String yourString = #"<element w:xpath=""/project[1]/someword[1]""/> <anothernode></anothernode> <another element w:xpath=""/project[1]/someotherword[1]""/>";
int yourNumber = 2;
string anotherString = string.Empty;
anotherString = Regex.Replace(yourString, #"(?<=project\[1\]/.*\[)\d(?=\]"")", yourNumber.ToString());
Matched groups are replaced using the $1, $2 syntax as follows :-
csharp> Regex.Replace("Meaning of life is 42", #"([^\d]*)(\d+)", "$1($2)");
"Meaning of life is (42)"
If you are new to regular expressions in .NET I recommend http://www.ultrapico.com/Expresso.htm
Also http://www.regular-expressions.info/dotnet.html has some good stuff for quick reference.
I've adapted yours to use a lookbehind and lookahead to only match a digit which is preceded by 'project[1]/xxxxx[' and followed by ']':
(?<=project\[1\]/.*\[)\d(?=\]")
Then, you can use:
String yourString = "project[1]/someword[1]";
int yourNumber = 2;
yourString = Regex.Replace(yourString, #"(?<=project\[1\]/.*\[)\d(?=\]"")", yourNumber.ToString());
I think maybe you were confused because Regex.Replace has lots of overloads which do slightly different things. I've used this one.
If you want to process the value of a captured group before replacing it, you'll have to separate the different parts of the string, make your modifications and put them back together.
string test = "project[1]/someword[1]\nproject[1]/someotherword[1]\n";
string result = string.Empty;
foreach (Match match in Regex.Matches(test, #"(project\[1\]/.*\[)([0-9])(\]\n)"))
{
result += match.Groups[1].Value;
result += (int.Parse(match.Groups[2].Value) + 1).ToString();
result += match.Groups[3].Value;
}
If you just want to replace text verbatim, it's easier: Regex.Replace(test, #"abc(.*)cba", #"cba$1abc").
you can use String.Replace (String, String)
for example
String.Replace ("someword[1]", "someword[2]")
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
How to escape all regex characters automatically using built-in .NET mechanism or something like that?
I am using Regex class to find match for a regular expression. See example below.
string pattern = "abc";
Regex regexp = new Regex(#"a\w", RegexOptions.None);
if (regexp.IsMatch(pattern))
{
MessageBox.Show("Found");
}
So, here Found will hit.
Now, in some cases, I still want to use Regex class but treat Regular Expression as plain string. So, for example, I will change pattern string to #"a\w" and need Regex class should find the match.
string pattern = #"a\w";
Regex regexp = new Regex(#"a\w", RegexOptions.None);
if (regexp.IsMatch(pattern))
{
MessageBox.Show("Found");
}
In the above case also, "Found" should hit.
So, the question is how to convert or treat Regular Expression into/as a plain string or something like that which can be used in a Regex Class? How to achieve the above code snippet scenario?
Note: - I do not want to use string.Contains, string.IndexOf, etc for plain text string matching.
You can "de-regex" your string through Regex.Escape. This will transform your pattern-to-search-for into the correctly escaped regex version.
string pattern = #"a\w";
Regex regexp = new Regex(Regex.Escape(#"a\w"), RegexOptions.None);
if (regexp.IsMatch(pattern))
{
MessageBox.Show("Found");
}
In the second case you don't want to search for what \w means in regex, you want to search for the literal \w, so you'd want:
string pattern = #"a\w";
Regex regexp = new Regex(#"a\\w", RegexOptions.None);
if (regexp.IsMatch(pattern))
{
MessageBox.Show("Found");
}
(Note the escaping).
You just need to escape your special characters like: #"a\\w"