I have a quite big string. In that big string, I want to get all UNIQUE words starts with @@ and ends with @@. Between @@ could be text, number or alphanumeric or anything.
Once I get all the UNIQUE words starting @@ and ends with @@, I want to replace each word with a value which matches a key in a different array.
Looking for the solution in C#.
The meta character “^” matches the beginning of a particular string i.e. it matches the first character of the string. For example, The expression “^\d” matches the string/line starting with a digit. The expression “^[a-z]” matches the string/line starting with a lower case alphabet.
The correct regex to use is ^\d+$. Because “start of string” must be matched before the match of \d+, and “end of string” must be matched right after it, the entire string must consist of digits for ^\d+$ to be able to match.
The regular expression \b[A]\w+ can be used to find all words in the text which start with A. The \b means to begin searching for matches at the beginning of words, the [A] means that these matches start with the letter A, and the \w+ means to match one or more word characters.
The Match-zero-or-more Operator ( * ) This operator repeats the smallest possible preceding regular expression as many times as necessary (including zero) to match the pattern. `*' represents this operator. For example, `o*' matches any string made up of zero or more `o' s.
Try this regex:
@@\b\S+?\b@@
Sample Code:
List<string> lst = new List<string>();
MatchCollection mcol = Regex.Matches(sampleString,@"@@\b\S+?\b@@");
foreach(Match m in mcol)
{
lst.Add(m.Tostring());
}
Here lst
contains matched value(s), compare each value and replace it as per you criteria.
Try following code (use Regex.Replace Method):
string s = @"@@Welcome@@ to @@reg-ex@@ @@world@@.";
Dictionary<string, string> sub = new Dictionary<string,string>{
{ "@@reg-ex@@", "regular expression" },
{ "@@world@@", "hell" },
};
Regex re = new Regex(@"@@.*?@@");
Console.WriteLine(re.Replace(s, x => {
string new_x;
return sub.TryGetValue(x.ToString(), out new_x) ? new_x : x.ToString();
}));
prints:
@@Welcome@@ to regular expression hell.
Example using Regex and Linq
string text = "@@bb@@@@cc@@@@sahasjah@@@@bb@@";
var matches = Regex.Matches(text, @"@@[^@]*@@");
var uniques = matches.Cast<Match>().Select(match => match.Value).ToList().Distinct();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With