Reputation: 58242
Given a string (as seen in the examples below), I would like to extract the following into three groups:
#
or not#
(if it exists) and the square brackets (if the [
)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.
#Sprite[abc]
Expected Result
Actual Result
#Sprite
Expected Result
Actual Result
Sprite
Expected Result
Actual Result
Sprite[abc]
Expected Result
Actual Result
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?
Upvotes: 1
Views: 100
Reputation: 40414
I've successfully use the following expression in python:
regex = re.compile(r'^(#)?(.*?)(?:\[(.*?)\])?$')
The problem was basically the question marks after the brackets (?
just after .*?
makes laziness difficult). The question mark now is for the whole expression, that is, (?:\[(.*?)\])?
.
Note: The (?:)
is used to avoid capturing the expression (I don't know if that's supported in the tool you're using).
Upvotes: 1
Reputation: 336368
Better be more specific instead of lazy :)
(#)?([^\[]*)(?:\[([^\]]*)\])?$
works on your examples. Translation:
(\#)? # Match # (optional)
([^\[]*) # Match any characters except [
(?: # Try to match...
\[ # [, followed by
([^\]]*) # any characters except ], followed by
\] # ]
)? # optionally
$ # Match end of string.
Upvotes: 2