Reputation: 2869
I have a regex:
@"\s*[\( \[ \{]\s*[a]\s*[n]\s*[\} \] \)]\s*"
than detects a string within exactly one opening and closing braces: ( [ {
even if there is any number of spaces in the string anywhere.
But the problem is that it also detects 'an'
if not within braces. How do I instruct regex to detect the string 'an' only within exactly one opening and closing braces.
abc an abc
should not match.
I want to match string like:
abc [ an ] abc
abc { a n } abc
abc ( a n ) abc
And can the brackets type be matched? I mean is it possible to match only those that have closing bracket same as opening bracket?
Upvotes: 0
Views: 598
Reputation: 444
Try removing the spaces in the square brackets. It's most likely matching those.
@"\s*[\(\[\{]\s*[a]\s*[n]\s*[\}\]\)]\s*"
Also, FWIW, this pattern doesn't check to ensure brackets are the same. It will get, e.g., [an}
or (an]
.
To do this, you'd need something like:
\(\s*a\s*n\s*\)|\{\s*a\s*n\s*\}|\[\s*a\s*n\s*\]
I'm sure there's a prettier way, but it's escaping me right now.
Upvotes: 3
Reputation: 2857
Try this:
^(\[ \( \{an\} \] \))$
This only allow the follow string:
[ ( {an} ] )
Upvotes: 1