- Home /
Negating specific sequences of characters in a Regex midway through a phrase that includes those same character sequences elsewhere
This is a weirdly specific question, but hours of experimentation have failed to yield a successful answer, so I'm turning here.
Some background: I have a method that processes character dialogue and selects phrases from among multiple variations; any time variants appear in unprocessed dialogue, they show up bracketed by square brackets and hyphens, with the variants delimited by forward slashes--like this:
[-Dialogue variant one./Dialogue variant two./Here's a third variant.-]
These are easily detected via a regular expression like this, which looks for a single opening square bracket, a single hyphen, any number of characters that don't include square brackets, and then a single hyphen and a single closing square bracket:
Regex altPhrases = new Regex( @"\[{1}\-{1}[^\[\]]*\-{1}\]{1}" );
Thanks to this "negated character set" in the middle...
[^\[\]]*
...I can use more than one set of variant phrases in any given line, and it will collect those sets of variants separately. For instance:
[-Dialogue variant one./Dialogue variant two.-] Furthermore, [-dialogue variant one./dialogue variant two.-]
Here, [-Dialogue variant one./Dialogue variant two.-] and [-dialogue variant one./dialogue variant two.-] will be collected as distinct, separate matches. So far, so good!
So what's the problem, you ask? Well, I actually use square brackets in dialogue sometimes to denote the actions of the speaker, and I want to be able to use them in dialogue variants. For instance:
[-[Bob grunts.] You don't say./[Bob scratches his head.] That's one hell of a story.-]
The negated character set I've been using will cause the RegEx not to match these variants, as they have square brackets between the [- and the -]. I need something that negates the specific character sequence [- , basically. Grouping the [ and - characters together in the negated character set, like this, does not seem to work:
Regex altPhrases = new Regex( @"\[{1}\-{1}[^(\[\-)]*\-{1}\]{1}" );
Any ideas?