0

I am looking to find matches in a string, perform an action on the match, and then replace the original match.

For example finding @yahoo in a string, looking on matching everything after the ampersand to the first white space. Of course there can be multiple values to match on in a single string so would be a for each match.

I'm thinking regex but am not sure on matching on everything after the ampersand to the first white space (the regex expression for this?). Or any other easier way of doing this?

2
  • 1
    You should post an example string. Where is the ampersand in @yahoo? Commented Jun 4, 2012 at 14:26
  • 2
    @ is called Commercial "at" sign, not an ampersand (which looks like this: &). Commented Jun 4, 2012 at 14:28

7 Answers 7

4

For this:

looking on matching everything after the ampersand to the first white space

regexp is @\S+.

Reference: Character Classes.

Sign up to request clarification or add additional context in comments.

Comments

2

Assuming you have your Regex correctly setup, you can utilize one of the overloads of Regex.Replace to include a MatchEvaluator delegate. The MatchEvaluator is a Func<Match,string> delegate (meaning any public string Method(Match match) method will work as input), with the return value being what you want to replace the original string with. The regex for the search is (@\S+) which means "Match the @ symbol, followed by any non-whitespace character (\S) at least once (+).

Regex.Replace(input, "(@\S+)", (match) => { /* Replace logic here. */ })

Running the above regex on the input @yahoo.com is going to be @simple for purposes of @matching., it matches on @yahoo.com, @simple and @matching. (notice that it includes punctiation on the @matching.).

Hope that helps!

2 Comments

Same problem of the answer from David. That pattern doesn't match the full string @yahoo.com but only @yahoo
+1, now your pattern is identical to the accepted answer, but you try to answer also to the other part of the question.
1

If you're writing in C#, a regex is probably your best option. The code is quite simple

MatchCollection matches = Regex.Matches(/*input*/, /*pattern*/)
foreach (Match m in matches)
{
    /*Do work here*/
}

For learning regular expressions and the syntax associated, I used http://www.regular-expressions.info/tutorial.html to get started. A lot of good information in there, and easy to read.

Comments

0

For example:

string str = "@yahoo aaaa bbb";
string replacedStr = str.Replace("@yahoo", "replacement");

Look to documentation: string.Replace

Comments

0

Try using String.Replace() function:

String x="lalala i like being @Yahoo , my Email is [email protected]";

x=x.Replace("@Yahoo","@Gmail");

X will be now : "lalala i like being @Gmail, my Email is [email protected]";

To know where the next space after "@Yahoo", use location variable, with String.IndexOf() and String.LastIndexOf().

int location=x.IndexOf("@Yahoo");//gets the location of the first "@Yahoo" of the string.

int SpaceLoc=x.IndexOf("@Yahoo",location);// gets the location of the first white space after the first "@Yahoo" of the string.

Hope that helps.

Comments

0

Do you mean ampersand & or at-symbol @?

This should do what you need: &([\S\.]+)\b

or for the at-symbol: @([\S\.]+)\b

2 Comments

This doesn't work for @yahoo.com (according to the OP request, you need to match everything till the first space)
@Steve Thanks. I modified it so that it will catch . as well.
0

I think a RegEx.Replace is your best bet. You can simply do something like this:

string input = "[email protected] is my email address";
string output = Regex.Replace(input, @"@\S+", new MatchEvaluator(evaluateMatch));

And you just need to define the evaluateMatch method, such as:

private string evaluateMatch(Match m)
{
    switch(m.Value)
    {
        case "@yahoo.com": 
            return "@google.com";
            break;
        default:
            return "@other.com";
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.