REGEX help

J

John Baro

I need to determine when multiple fonts are selected in a richtextbox.
A font is indicated by \fcharset(N) where (N) is a number. (To the best of
my knowledge)

I can use this statement

int Matches = 0;
for(Match m = Regex.Match(rtfTextEdit.SelectedRtf, @"\\fcharset\d");
m.Success; m = m.NextMatch())
{
Matches++;
if(Matches == 2)
{
//multiple fonts selected
break;
}
}

but there has to be an easier way.
I (surely) can specify that I want to match 2 or more \fcharset(n) instances
before a match is set.

This also breaks when I type in \fcharset0 in the textbox which will put
\\\\fcharset0 in the rtf.
Therefore it will match \fcharset0 twice, even though it is the same font.
I therefore need to exclude any instance with \\ in front.
Looking at msdn the ~(X) expression should do this but I cannot get it to
work.
ie ~(\\\\)\\fcharset0
or
~(\\)\\fcharset0

I have downloaded eric gunnersons regex tester from gotdotnet but I cannot
seem to get it to work properly with the above constructs.
It does not seem to recognise the ~(X) construct.

There seem to be major differences between MS regex and say perl regex?

Any help would be muchly appreciated.

Cheers
JB

PS I know I could probably do this with split and replace but I want to
figure out regex. :)
 
J

John Baro

===
Looking at msdn the ~(X) expression should do this but I cannot get it to
work.
ie ~(\\\\)\\fcharset0
or
~(\\)\\fcharset0

I have downloaded eric gunnersons regex tester from gotdotnet but I cannot
seem to get it to work properly with the above constructs.
It does not seem to recognise the ~(X) construct.

And it seems that the c not operator (!) works.
ie real(?!ity)
will not match reality
but will match real.

It does not work at the beginning however
ie
(?!ity)real
will match both
real
and
ityreal

Man am I confused

JB :(
 
C

Chris R. Timmons

I need to determine when multiple fonts are selected in a
richtextbox. A font is indicated by \fcharset(N) where (N) is a
number. (To the best of my knowledge)

I can use this statement

int Matches = 0;
for(Match m = Regex.Match(rtfTextEdit.SelectedRtf,
@"\\fcharset\d"); m.Success; m = m.NextMatch())
{
Matches++;
if(Matches == 2)
{
//multiple fonts selected
break;
}
}

but there has to be an easier way.
I (surely) can specify that I want to match 2 or more
\fcharset(n) instances before a match is set.

This also breaks when I type in \fcharset0 in the textbox which
will put \\\\fcharset0 in the rtf.
Therefore it will match \fcharset0 twice, even though it is the
same font. I therefore need to exclude any instance with \\ in
front.

John,

You can use a negative lookbehind to achieve the match you want:

(?<!\\)\\fcharset\d+

This will match \fcharset1, but not \\fcharset1.

The Regex.Matches method can be used to detect multiple matches in a
regular expression.

bool multipleMatches = Regex.Matches(rtfTextEdit.SelectedRtf,
@"(?<!\\)\\fcharset\d+",
RegexOptions.IgnoreCase |
RegexOptions.IgnorePatternWhitespace |
RegexOptions.Singleline).Count > 1;
Looking at msdn the ~(X) expression should do this but I
cannot get it to work.
ie ~(\\\\)\\fcharset0
or
~(\\)\\fcharset0

I have downloaded eric gunnersons regex tester from gotdotnet
but I cannot seem to get it to work properly with the above
constructs. It does not seem to recognise the ~(X) construct.

The ~(X) construct is specific to the Visual Studio IDE. It does not
work in the .Net framework.
And it seems that the c not operator (!) works.
ie real(?!ity)
will not match reality
but will match real.

It does not work at the beginning however
ie
(?!ity)real
will match both
real
and
ityreal

(?! indicates the beginning of a "zero-width negative lookahead
assertion" construct. A regex like "real(?!ity)" can be read as
"match the characters 'real', if and only if the letters 'ity' do not
appear immediately to the right of the 'l'."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top