开发者

How to Extract the Word Following a Symbol?

I have a string that could have any sentence in it but somewhere in that string will be the @ symbol, followed by an attached word, sort of like @username you see on some sites.

so maybe the string is "hey how are you" or it's "@john hey how are you".

IF there's an "@" in the string i want to pull what comes immediately after it into its own 开发者_如何学Pythonnew string.

in this instance how can i pull "john" into a different string so i could theoretically notify this person of his new message? i'm trying to play with string.contains or .replace but i'm pretty new and having a hard time.

this btw is in c# asp.net


You can use the Substring and IndexOf methods together to achieve this.

I hope this helps.

Thanks, Damian


Here's how you do it without regex:

string s = "hi there @john how are you";

string getTag(string s)
{
    int atSign = s.IndexOf("@");

    if (atSign == -1) return "";

    // start at @, stop at sentence or phrase end
    // I'm assuming this is English, of course
    // so we leave in ' and -
    int wordEnd = s.IndexOfAny(" .,;:!?", atSign); 

    if (wordEnd > -1)
        return s.Substring(atSign, wordEnd - atSign);
    else
        return s.Substring(atSign);

}


You should really learn regular expressions. This will work for you:

using System.Text.RegularExpressions;

var res = Regex.Match("hey @john how are you", @"@(\S+)");

if (res.Success)
{
    //john
    var name = res.Groups[1].Value;
}

Finds the first occurrence. If you want to find all you can use Regex.Matches. \S means anything else than a whitespace. This means it also make hey @john, how are you => john, and @john123 => john123 which may be wrong. Maybe [a-zA-Z] or similar would suit you better (depends on which characters the usernames is made of). If you would give more examples, I could tune it :)

I can recommend this page:

http://www.regular-expressions.info/

and this tool where you can test your statements:

http://regexlib.com/RESilverlight.aspx


The best way to solve this is using Regular Expressions. You can find a great resource here.

Using RegEx, you can search for the pattern you are after. I always have to refer to some documentation to write one...

Here is a pattern to start with - "@(\w+)" - the @ will get matched, and then the parentheses will indicate that you want what comes after. The "\w" means you want only word characters to match (a-z or A-Z), and the "+" indicates that there should be one or more word characters in a row.


You can try Regex...

I think will be something like this

string userName = Regex.Match(yourString, "@(.+)\\s").Groups[1].Value;


RegularExpressions. Dont know C#, but the RegEx would be

/(@[\w]+) / - Everything in the parans is captured in a special variable, or attached to RegEx object.


Use this:

var r = new Regex(@"@\w+");
foreach (Match m in r.Matches(stringToSearch))
    DoSomething(m.Value);

DoSomething(string foundName) is a function that handles name (found after @).
This will find all @names in stringToSearch

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜