How does this regular expression retrieve a company name?

Could you explain why the conclusion Console.WriteLine(m.Groups[1]);will be Contoso, Inc? And could you also tell about the corresponding steps of this example? Thank.

PS: I am not familiar with the concept of groups

string input = "Company Name: Contoso, Inc."; 
Match m = Regex.Match(input, @"Company Name: (.*$)"); 
Console.WriteLine(m.Groups[1]);
+3
source share
5 answers

I am not familiar with the concept of groups

A group is part of a regular expression that is preserved when matching is performed. To declare a group in your regular expression, you put part of the expression between parentheses. This part is then stored within the group.

Groups are numbered (although they can also be given explicit names) from left to right from outside to inside. The zero-go group is a complete coincidence.

, , .. "Company Name:" .

+2

, , , , . [0] - , , [i] ( i > 0) n- . .

+1

. : , , Company Name:, , .*, , $. .*$ , , . , . Groups[0] ( ), (.*$) Groups[1].

+1

, :)

" : (. * $)" , RegExOptions.SingleLine ( ).

" :" , ( "^ :" , , " :" . (^ = )

(.*$) .

".*$", : ".", 0 "*", "$"

Clear?:)

+1

If you are interested in learning the ins and outs of regular expressions (along with how different implementations depend on the language and platform), I definitely recommend Mastering Regular Expressions from O'Reilly .

+1
source

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


All Articles