Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all words starts with @@ and ends with @@ in long string

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#.

like image 782
Shiras Avatar asked Jul 30 '13 07:07

Shiras


People also ask

How do you search for a RegEx pattern at the beginning of a string?

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.

What is the RegEx pattern for end of string?

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.

How do you search for a word in RegEx?

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.

What does * do in RegEx?

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.


3 Answers

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.

Sample live demo

like image 105
NeverHopeless Avatar answered Oct 05 '22 01:10

NeverHopeless


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.
like image 45
falsetru Avatar answered Oct 04 '22 23:10

falsetru


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();
like image 34
tray2002 Avatar answered Oct 05 '22 01:10

tray2002