Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by gantolandon · Mar 15, 2015 at 11:26 PM · c#regex

Regex capturing groups find whole match instead

I'm trying to find pairs of numbers in parentheses, then parse them. For that reason I constructed a regular expression with capturing groups inside this snippet of code:

             Regex rgx = new Regex(@"\(([0-9]*)\, ?([0-9]*)\)");
             MatchCollection matches = rgx.Matches(current["EmptyFields"].InnerText);
 
             if (matches.Count > 0) {
                 foreach (Match match in matches) {
                     CaptureCollection captures = match.Captures;
                     emptyFields.Add(new GameBoardCoords(Int32.Parse (captures[0].Value), Int32.Parse (captures[1].Value)));
                 }
             }


The intended result are two capturing groups, each with one member of a pair. For example, for "(0,0)", there should be two capturing groups with "0" and "0". This is how it works when I test the expression in the regex explorer.

For some reason instead, this code generates one capturing group with the entire match - "(0,0)" in our example. Is there something I missed here?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by asafsitner · Mar 16, 2015 at 08:36 AM

Your regex works perfectly - try it out in Regexr

So why does it not work?
Because you're accessing the wrong groups.
In a regex match, group 0 is always the whole match!
You can see it in Regexr by hovering over the matches and see which group is which part of the match.

Instead you need to access groups 1 and 2 in your code like so:

 emptyFields.Add(new GameBoardCoords(Int32.Parse (captures[1].Value), Int32.Parse (captures[2].Value)));

You may want to change your regex to force number input however, or you might run into an empty string. Basically just replace the * with + in the pattern like so:

 Regex rgx = new Regex(@"\(([0-9]+)\, ?([0-9]+)\)");

Alternatively, either check for string.IsNullOrRmpty or use int.TryParse like so:

 if (matches.Count > 0)
 {
     foreach (Match match in matches)
     {
         CaptureCollection captures = match.Captures;
 
         // either check for empty strings and fill with default value
         int coord1 = string.IsNullOrEmpty(captures[1].Value) ? 0 : Int32.Parse(captures[1].Value);
         int coord2 = string.IsNullOrEmpty(captures[2].Value) ? 0 : Int32.Parse(captures[2].Value);
 
         // or use try parse
         if (!Int32.TryParse(captures[1].Value, out coord1)) coord1 = 0;
         if (!Int32.TryParse(captures[2].Value, out coord2)) coord2 = 0;
 
         emptyFields.Add(new GameBoardCoords(coord1, coord2));
     }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by dethtoll · Mar 16, 2015 at 08:24 AM

In C#, the backslash () character is a special 'escape' character used for representing special characters such as newlines (\n). In order to represent a backslash, you must use a double backslash (\) instead.

This can be slightly confusing, since the backslash is being used as an escape character in the regex itself - eg. to represent a left paren instead of the beginning of a capture. But the regex won't even see the backslash in this case because c# is 'eating' it beforehand.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image asafsitner · Mar 16, 2015 at 08:29 AM 0
Share

That is not entirely true - notice the @ symbol indicating a verbatim string, so the regex will pick it up exactly as it is.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Flip over an object (smooth transition) 3 Answers

Replace with a value from hashtable 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges