-1

I have a Regex expression:

@"(?<one>M\/S\s(\w+?\s)+(?<two>(U22334)))" 

...which is tested against the string:

"M/S ALPHA DAILY LIVING PRODUCTS INDIA PRIVATE LIMITED  U22334"

My desktop Hero Regex tester shows that there is one match and four groups as follows.

1: LIMITED

2: U22334

one: M/S ALPHA DAILY LIVING PRODUCTS INDIA PRIVATE LIMITED  U22334

two: U22334

I would like to know how the these groups are determined. Any help would be appreciated very much.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Unnikrishnan
  • 523
  • 1
  • 8
  • 18
  • .NET differs from from other flavors in that non-named groups are numbered independently from named groups. Is that what you're asking about? – Alan Moore Oct 02 '16 at 07:49
  • I don't know whether it is a duplicate. This problem has destroyed my two days. That is the reason I posted this question. To an extent the answer of Rudis was satisfying to me although I am yet to gain proficiency with iteration through foreach loop. – Unnikrishnan Oct 02 '16 at 08:06

1 Answers1

1
  • Group 1 is defined by (\w+?\s) and it captures only last iteration
  • Group 2 is defined by (U22334)
  • Named group one is defined with whole regex definition
  • Named group two is defined by (?<two>(U22334))

For detailed explanation you can use e.g. page regex101.com (this link is with your regex pattern and tested string)

Rudis
  • 1,177
  • 15
  • 30
  • Now I understand a bit from the explanation of Rudis. Thank you @Rudis. So in my case if I iterate with a foreach loop, will it go through the loop four times in my case. I am so confused about its iteration through foreach....loop. – Unnikrishnan Oct 02 '16 at 08:01
  • You can read named groups directly by name - http://stackoverflow.com/questions/906493/how-do-i-access-named-capturing-groups-in-a-net-regex – Rudis Oct 02 '16 at 08:05
  • Okay. Thank you. And I will do that. After my problem is solved, I will write what I did as suggested by you. – Unnikrishnan Oct 02 '16 at 08:07
  • I would have loved to post how I understood my problem as an answer to those who are starting out with Regex capturing group. But it seems that since this is marked duplicate I am unable to do it. – Unnikrishnan Oct 03 '16 at 03:20