How to extract a word after a character?

I have a line in which there can be any sentence, but somewhere in that line there will be a @ symbol followed by an attached word, like @username, which you see on some sites.

so maybe the string “hey how are you” or “@john hey how are you”.

IF there is the string "@" in the line that I want to pull out, which comes immediately after it in its own new line.

in this case, how can I pull "john" to another line to theoretically notify this person of his new message? I'm trying to play with string.contains or .replace, but I'm pretty newbie and hard.

this bit is in c # asp.net

+3
source share
7

. :

using System.Text.RegularExpressions;

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

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

. , Regex.Matches. \S - , . , hey @john, how are you = > john, @john123 = > john123, . , [a-zA-Z] ( , ). , :)

:

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

, :

http://regexlib.com/RESilverlight.aspx

+3

IndexOf .

, .

, Damian

+6

:

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);

}
+5

- . .

RegEx, , . , ...

- "@(\ w +)" - @ , , , . "\ W" , (a-z A-Z), "+" , .

+1

RegularExpressions. #, RegEx

/(@[\w]+) / - Everything in paranas is captured in a special variable or bound to a RegEx object.

+1
source

You can try regex ...

I think there will be something like this

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

Use this:

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

DoSomething(string foundName)is a function that processes the name (found after @).
This will find all @names instringToSearch

0
source

Source: https://habr.com/ru/post/1765889/


All Articles