-3

My task is to find a specific word from a line by splitting with punctuation that can be anything (even a letter or any character in general). After finding the word, I have to return the word with punctuation that goes after it. I have come up with idea to firstly split line by punctuation, find each string that goes in between punctuation marks and then to split the same line only with words, but for some reason i only get the regular expression.

public static string FindWord2InLine(string line, string punctuation)
{
    string[] parts = Regex.Split(line,"[" + punctuation + "]+");
    string temp = "";
    for (int i = 0; i < parts.Length; i++)
    {
        temp += Regex.Split(line, "[" + parts[i] + "]+");
    }
    return temp;
}
Tautviss
  • 1
  • 1
  • 4
    Could you please provide a test case? What exact input have you got and what is the expected output? – Wiktor Stribiżew Dec 18 '19 at 13:06
  • Are you searching for a particular word after you spit the line? Or are you just returning each word with the punctuation you used for splitting? – Ndubuisi Jr Dec 18 '19 at 13:39
  • Lets say you want "Hello ,; /. World!" to be split by " ,; /. " and you want Hello as your word so the output should be "Hello ,; /. " – Tautviss Dec 18 '19 at 21:11
  • What if the input is `"I just ,; /. want to say Hello ,; /. World!"` ... what should the output be? – Brett Dec 19 '19 at 09:59

3 Answers3

-1

You can use this code:

public static string FindWord2InLine(string line, string punctuation)
{
    var matches = Regex.Matches(line, $"\\w+[{punctuation}]");  //match word with punctuation after it
    string temp = "";
    foreach (var match in matches)
    {
        temp += match;  // perform action with word and punctuation
    }
    return temp;
}

Return of calling FindWord2InLine("foo, bar!,..,/ baz.", ",.!:") is foo,bar!baz.

-1

I think the the regex you want is \b[\w-]+?\b[,.] where ,. are your punctuation characters.

To break down the regex a little more:

\b is a word boundary. [\w-] matches a word character (letter / number) or a hyphen. +? matches the characters/hyphens at least once but as few as possible before the next boundary

Given the input Some words here. With some, punctuation... this makes three matches:

 here.
 some,
 punctuation.

Since you are returning only a single string from your function, quite how you determine which of these matches to return is up to you.

Brett
  • 1,352
  • 8
  • 13
  • Don't mind the down-vote, but a little explanation would be nice. What is it about the answer that you think needs improvement? – Brett Dec 18 '19 at 15:55
  • Wasn't me that downvoted, but as far as i can see you talk about using words and other characters with \b and \w which goes against the rule that any kind of character can be used as punctuation. – Tautviss Dec 18 '19 at 21:42
  • @Tautviss - I take your point with regards the hyphen ... that's an embellishment too far, perhaps. Since OP's question talks about finding a word I'd maintain that using word boundaries (`/b`) and word characters (`/w`) is a fair approach. Question has been commented on now by OP, indicating that the punctuation is a full sequence of characters rather than a class/set of possible punctuation characters ... so we'll aim to get to the bottom of what the problem really is! – Brett Dec 19 '19 at 10:05
-1

Found out, that " Match withPunctuation = Regex.Match(line, temp + "[" + punctuation + "]+"); " Does The job

public static string FindWord2InLine(string line, string punctuation)

    {
        string[] parts = Regex.Split(line, "[" + punctuation + "]+");
        int max = 0;
        string temp = "";
        for (int i = 0; i < parts.Length; i++)
        {
            if (parts[i].Length > max) //If Longest Word
            {
                if ((parts[i].Length + 1) / 2 <= NumberOfDigits(parts[i])) // If atleast half of numbers are digits
                {
                    temp = parts[i];
                    max = parts[i].Length;
                }
            }
        }
        Match withPunctuation = Regex.Match(line, temp + "[" + punctuation + "]+"); // matches line with word and punctuation
        return withPunctuation.ToString();
    }
Tautviss
  • 1
  • 1