Reputation: 2868
For example, I have the following string:
-----ABC-----
And use
regexp {^[\-]+.*[\-]+} $string
can match the above string. But if I want to match fixed number of -
, e.g. 5 times, how to do that? I tried
regexp {^[\-]{5}.*[\-]{5}} $string
But it doesn't work.
Upvotes: 1
Views: 279
Reputation: 52185
Assuming that by does not work you mean that it also matches something like so: -----ABC--------------
, you could change this: {^[\-]{5}.*[\-]{5}}
to this: {^[\-]{5}[^-]*[\-]{5}$}
.
The main difference is that I am specifying that mid section, that is, the section which in your example contains ABC
should not be made out of dashes, so it will match 5 dashes from the beginning of the string (^[\-]{5}
), followed by 0 or more characters which are not dashes ([^-]*
), followed by 5 more dashes and a string termination ([\-]{5}$
).
Upvotes: 1
Reputation: 4659
The .*
part matches the -
as well. I would change it to this:
^-{5}[^-]*-{5}$
[^-]*
means "any character except a -
. (you don't have to put the -
in []
if it's the only allowed character)
Upvotes: 2