Given a string (as seen in the examples below), I would like to extract the following into three groups:
- Group 1: Is the first character a
#or not - Group 2: Capture the string between the
#(if it exists) and the square brackets (if the[) - Group 3: Capture the contents of the square brackets (without the square brackets)
At this stage I have the following regular expression:
/^(#)?(.*?)\[?(.*?)\]?$/
I am using http://gskinner.com/RegExr/ as my testing tool with multiline and global turned on.
Example 1:
#Sprite[abc]
Expected Result
- Group 1: #
- Group 2: Sprite
- Group 3: abc
Actual Result
- Group 1: #
- Group 2: // Empty, not NO MATCH
- Group 3: Sprite[abc // No trailing ]
Example 2:
#Sprite
Expected Result
- Group 1: #
- Group 2: Sprite
- Group 3: [NO MATCH]
Actual Result
- Group 1: #
- Group 2:
- Group 3: Sprite
Example 3:
Sprite
Expected Result
- Group 1: [NO MATCH]
- Group 2: Sprite
- Group 3: [NO MATCH]
Actual Result
- Group 1: [NO MATCH]
- Group 2: // empty
- Group 3: Sprite
Example 4:
Sprite[abc]
Expected Result
- Group 1: [NO MATCH]
- Group 2: Sprite
- Group 3: abc
Actual Result
- Group 1: [NO MATCH]
- Group 2: // empty
- Group 3: Sprite[abc
To me it feels like the lazy match in the expression above isn’t well being lazy, shouldn’t it hit the [ and break out, group, and move on?
Better be more specific instead of lazy 🙂
works on your examples. Translation: