Pergunta

I'm trying to retrieve a particular string with the Regex Expression from a sentence. This is my initial Regex:

   Regex Reg = new Regex(@"^[KB|kb](\w+)[-]?(\w+)$");

  From input of :
  word word word (KB92922-WMP9) word.
  word word word (KB929222910) word word.
  word word kb28928282 word word.

It should be able to retrieve:

  KB92922-WMP9
  KB929222910
  kb28928282

Treat them as sentences, and we retrieve data by passing a sentence one at a time. Retrieving its data Without the parentheses.

How is this going to work? I have no idea. I think I've got the Regex Expression right but I'm not so sure how to retrieve the data.

Foi útil?

Solução

You are looking for Regex.Matches, (assuming that one sentence might contain both KB12345 and KB98765). it is similar to Regex.Match, but will give you all matching substrings.

If you have the entire text as one string, it's probably not necessary to split it into sentences. Just getting all matches in the entire string will get you the same result as collecting all matches in all sentences within the same string.

Edit: updating the regex you are using

Regex Reg = new Regex(@"(KB\d+(?:-[\w\d]+)?)", RegexOptions.IgnoreCase);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top