change FONT color using Regex.Split

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

i'm trying to use Regex to match a 4 number group pattern.
once a match is found, write it to RichTextBox in red.

test data:
This is my 4567 test data.
1234this is another line of data.
Here's two more 9876 5432.
What a headache!!!


result should be the same as the test data above.
except all the 4 group number should be in RED.
it should match the white space, letter case, punctuation, etc.

i tried the SPLIT method in Regex but can't seem to get it to work.
the goal is to get the result to work.
doesn't matter if it's Regex or not.

i think i got confused on how to iterate thru each token (?) and then match
the Regex pattern and do something with it accordingly.

here's what i wanted to achieve.
1) if the Regex pattern is found, write it on screen with RED font.
2) if the Regex pattern doesn't match, write it in black.
3) keep all mechanics, punctuation, etc. as the original text.


appreciate for any inputs.


TIA,
Ada
 
This one seems to work:

string src = @"test data:
This is my 4567 test data.
1234this is another line of data.
Here's two more 9876 5432.
What a headache!!!
";

src.Replace(@"\", @"\\");
src.Replace(@"{", @"\{");
src.Replace(@"}", @"\}");
src = System.Text.RegularExpressions.Regex.Replace
(src, @"((?<=\D|\A)\d{4}(?=\D|\z))", @"{\cf2 $1}");

richTextBox1.Rtf =
@"{\rtf1\ansi{\colortbl;\red0\green0\blue0;\red255\green0\blue0;}"+src+"}";

However, I didn't have that much time to test it...

Niki
 
Back
Top