C# regular expression to find custom markers and take content - c#

I have a string:
productDescription
In it are some custom tags such as:
[MM][/MM]
For example the string might read:
This product is [MM]1000[/MM] long
Using a regular expression how can I find those MM tags, take the content of them and replace everything with another string? So for example the output should be:
This product is 10 cm long

I think you'll need to pass a delegate to the regex for that.
Regex theRegex = new Regex(#"\[MM\](\d+)\[/MM\]");
text = theRegex.Replace(text, delegate(Match thisMatch)
{
int mmLength = Convert.ToInt32(thisMatch.Groups[1].Value);
int cmLength = mmLength / 10;
return cmLength.ToString() + "cm";
});

Using RegexDesigner.NET:
using System.Text.RegularExpressions;
// Regex Replace code for C#
void ReplaceRegex()
{
// Regex search and replace
RegexOptions options = RegexOptions.None;
Regex regex = new Regex(#"\[MM\](?<value>.*)\[\/MM\]", options);
string input = #"[MM]1000[/MM]";
string replacement = #"10 cm";
string result = regex.Replace(input, replacement);
// TODO: Do something with result
System.Windows.Forms.MessageBox.Show(result, "Replace");
}
Or if you want the orginal text back in the replacement:
Regex regex = new Regex(#"\[MM\](?<theText>.*)\[\/MM\]", options);
string replacement = #"${theText} cm";

A regex like this
\[(\w+)\](\d+)\[\/\w+\]
will find and collect the units (like MM) and the values (like 1000). That would at least allow you to use the pairs of parts intelligently to do the conversion. You could then put the replacement string together, and do a straightforward string replacement, because you know the exact string you're replacing.
I don't think you can do a simple RegEx.Replace, because you don't know the replacement string at the point you do the search.

Regex rex = new Regex(#"\[MM\]([0-9]+)\[\/MM\]");
string s = "This product is [MM]1000[/MM] long";
MatchCollection mc = rex.Matches(s);
Will match only integers.
mc[n].Groups[1].Value;
will then give the numeric part of nth match.

Related

How can I replace "XX,XXX" with "XX XXX"?

I need to replace string like "XX,XXX" with "XX XXX". The string "XX,XXX" is in another string, e.g:
"-1299-5,"XXX,XXXX",trft,4,0,10800"
The string is fetched from a text file. I want to split the string by ",". But the comma in the substring led to the wrong result.
The X represents a char. I think regex can help, who can give me the right regex expression.
This expression,
(.*"[^,]*),([^,]*".*)
with a replacement of $1 $2 might work.
Demo
Example
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = #"(.*""[^,]*),([^,]*"".*)";
string substitution = #"\1 \2";
string input = #"-1299-5,""XXX,XXXX"",trft,4,0,10800";
RegexOptions options = RegexOptions.Multiline;
Regex regex = new Regex(pattern, options);
string result = regex.Replace(input, substitution);
}
}
Simply, use 'Replace' to replace char from your string.
var test = "XXX,XXXX";
var filtered = test.Replace(',', ' ');
Console.WriteLine(filtered);
Output :
XXX XXXX

Regex to parse URL from an excel formula

I have a formula in excel which upon reading from C# code looks like this
"=HYPERLINK(CONCATENATE(\"https://abc.efghi.rtyui.com/#/wqeqwq/\",#REF!,\"/asdasd\"), \"View asdas\")"
I want to use regex to fetch the URL from this string, i.e.
https://abc.efghi.rtyui.com/#/wqeqwq/#REF!/asdasd
The url can be different but the format of the formula will remain the same.
"=HYPERLINK(CONCATENATE(\"{SOME_STRING}\",#REF!,\"{SOME_STRING}\"), \"View asdas\")"
Try it like this:
(?<=HYPERLINK\(CONCATENATE\(")[^"]+
Demo
The positive lookbehind allows us to skip part in-front of the URL from the full match.
If you have an arbitrary number of whitespace in-between add some \s*, e.g. see this example that also shows the escaped = at the beginning of the string.
Sample Code:
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = #"(?<=HYPERLINK\(CONCATENATE\("")[^""]+";
string input = #"=HYPERLINK(CONCATENATE(""https://abc.efghi.rtyui.com/#/wqeqwq/"",#REF!,""/asdasd""), ""View asdas"")";
RegexOptions options = RegexOptions.Multiline;
foreach (Match m in Regex.Matches(input, pattern, options))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
}
}
Addendum: Here is another technique that uses capturing groups and regex Replace to extract the resulting URL string (after CONCATENATE would have happened):
^\=HYPERLINK\(CONCATENATE\("([^"]+)",([^,]+),"([^"]+)".*$
Demo2
string pattern = #"^\=HYPERLINK\(CONCATENATE\(""([^""]+)"",([^,]+),""([^""]+)"".*$";
string substitution = #"$1$2$3";
string input = #"=HYPERLINK(CONCATENATE(""https://abc.efghi.rtyui.com/#/wqeqwq/"",#REF!,""/asdasd""), ""View asdas"")";
Regex regex = new Regex(pattern);
string result = regex.Replace(input, substitution, 1);
You can extract the URL from the formula using capturing groups in regular expression as given below:
string inputString = "=HYPERLINK(CONCATENATE(\"https://abc.efghi.rtyui.com/#/wqeqwq/\",#REF!,\"/asdasd\"), \"View asdas\")";
string regex = "CONCATENATE\\(\"([\\S]+)\",#REF!,\"([\\S]+)\"\\)";
Regex substringRegex = new Regex(regex, RegexOptions.IgnoreCase);
Match substringMatch = substringRegex.Match(inputString);
if (substringMatch.Success)
{
string url = substringMatch.Groups[1].Value + "#REF!" + substringMatch.Groups[2].Value;
}
I have defined two capturing groups in my regular expression. One for extracting part of the URL before #REF! and the other for extracting part of the URL after #REF!. Then I am concatenating all the extracted parts with #REF! to get the final URL.

c#: how to match a variable string followed by a space and currency and pull the currency?

I'm trying to match the following cases and pull the number value:
"b 30.00"
"bill 30.00"
"bill 30"
"b 30"
I've tried:
var regex = new Regex("^b(?-i:ill)?$ ^$?d+(.d{2})?$", RegexOptions.IgnoreCase);
However, this doesn't seem to return a match, and I'm not sure how to pull the digit.
You haven't well understand how to use anchors ^ & $, read about this.
var regex = new Regex(#"^[Bb](?:ill)? \d+(?:\.\d{2})?$");
or better since you only need ascii digits (and not all possible digits of the world):
var regex = new Regex(#"^[Bb](?:ill)? [0-9]+(?:\.[0-9]{2})?$");
If you want to figure a literal . you must escape it (same thing for a literal $). Note the use of a verbatim string to avoid double backslashes.
Feel free to add capture groups around what you want to capture.
You didn't mention if RegEx is actually required to accomplish your goal. If RegEx is not required, and you know that your string is in a specific format, you could just split the string:
string val = "bill 30.00";
string[] split = val.Split(' ');
string name = string.Empty;
decimal currency = 0m;
if (split.Length > 1)
{
name = split[0];
decimal.TryParse(split[1], out currency);
}
new Regex (#"\b\d+(.\d {2})*") should give you what you want
Just try the code
string Value = "bill 30.00";
string resultString = Regex.Match(Value, #"\d+").Value;

Regex, match text inside a tag, then match all text not in that tag both from same string?

I suck at Regex's and am surprised I was able to get as far as I did by myself.
So far I've got this:
string text = "Whoa here is some very cool text.<phone>222-222-5555</phone><timesCalled>6</timescalled>";
Regex phoneRegex = new Regex(#"<phone>(.*?)<\/phone>");
Regex calledRegex = new Regex(#"<timesCalled>(.*?)<\/timesCalled>");
string phone = phoneRegex.Match(text).Value;
string timesCalled = calledRegex.Match(text).Value;
Both of these give me the full tags and the value inside, how do I make it so it only returns what is inside the tag? Also I need a final regex that would return all the text not inside those tags, so Whoa here is some very cool text. from the above example. The special tags would always appear after the normal text, if that matters.
Edit: Thanks for the answers all, I still need the final regex though (bolded above).
So far I tried this:
string pattern = #"^" + phoneRegex.Match(text).Value + calledRegex.Match(text).Value;
Regex textRegex = new Regex(pattern);
string normalText = textRegex.Match(text).Groups[1].Value;
but that is returning nothing.
You want to get the value of the group:
calledregex.Match(text).Groups[1].Value
Groups are 1-based.
How about reading/parsing XML with an Xml class?
var doc = XElement.Parse("<root>" + text + "</root>");
string phone = doc.Descendants("phone").First().Value;
Here is my suggestion, gives you the chance to serach for more tags with values.
string text = "Whoa here is some very cool text.<phone>222-222-5555</phone><timesCalled>6</timesCalled>";
Regex regex = new Regex(#"<(?<tag>[^>]*)>(?<value>[^<]*)</\k<tag>>");
Match match = regex.Match(text);
string phone = match.Groups["value"].Captures[match.Groups["tag"].Captures.OfType<Capture>().FirstOrDefault(item => item.Value == "phone").Index].Value;
string timesCalled = match.Groups["value"].Captures[match.Groups["tag"].Captures.OfType<Capture>().FirstOrDefault(item => item.Value == "timesCalled").Index].Value;
The Value of a match is everything that matched the pattern. If you only want the grouped contents (the stuff within the tags), you'd have to access them through the Groups property.
string phone = phoneRegex.Match(text).Groups[1].Value;
string timesCalled = calledregex.Match(text).Groups[1].Value;
In the case of inline xml/html I would also ignore case, tag capitalization can be wonky sometimes.
string text = "Whoa here is some very cool text.<phone>222-222-5555</phone><timesCalled>6</timesCalled>";
Regex phoneRegex = new Regex(#"<phone>(.*?)<\/phone>", RegexOptions.IgnoreCase);
Regex calledRegex = new Regex(#"<timesCalled>(.*?)<\/timesCalled>", RegexOptions.IgnoreCase);
string phone = phoneRegex.Match(text).Groups[1].Value;
string timesCalled = calledRegex.Match(text).Groups[1].Value;

Csharp: Find & Replace string match between specific characters || in a string using Regex

I have a string like:
string originalStringBefore = "http://www.abc.com?a=||ThisIsForRndNumber||&qq=hello&jj=||ThisIsForRndNumberAlso||";
I want every string which comes between || to be replaced with a random number.
Now, the random number generation is easy, but i can't find the way to write a Regular expression to search the string using a pattern and replace it.
I don't want to do it by string manipulation functions.
Expected Solution/ outcome:
string originalStringAfter = "http://www.abc.com?a=||254877787||&qq=hello&jj=||6594741454||";
string originalStringBefore = "http://www.abc.com?a=||ThisIsForRndNumber||&qq=hello&jj=||ThisIsForRndNumberAlso||";
Random r = new Random();
Regex rgx = new Regex(#"\|\|.*?\|\|");
Console.WriteLine(rgx.Replace(originalStringBefore, "||" + r.Next(int.MaxValue) + "||"));
It looks like you want this regex:
(?<=\|\|)\w+(?=\|\|)
which finds alphanumeric between || and leaves strings that contain non-alphanumeric characters (like &) alone.
Then, in C#:
public String ComputeReplacement(Match m) {
return RandomNumberString();
}
resultString = Regex.Replace(subjectString, #"(?<=\|\|)\w+(?=\|\|)", new MatchEvaluator(ComputeReplacement));
Please find the regular expression replace command pasted below
string result = Regex.Replace(originalStringBefore, #"\|\|.*?\|\|","||ReplaceCharactors||");
how ever to write reguler expression's go through this site hope this wil help you
Thanks.

Categories