CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Colouring words

 



elziko
newbie

Nov 20, 2001, 2:22 AM

Post #1 of 2 (4782 views)
Colouring words Can't Post

Firstly I'll explain what I'm doing:

I'm writing a code colouring function that will colour a words in a rich text box that our found in a list. This is a basically a syntax colouring function. I'm using regular expressions as below in Visual Basic so I know this isn't exactly the best forum to post this in but Regular expressions are still Regular Expressions!



re.IgnoreCase = True
re.Global = True
re.Pattern = strWordList

ReplacementString = "{\rtf1\ansi\ansicpg1252\deff0\deftab720{\fonttbl{\f" & strTableRef & strFontTable & "}}{\colortbl" & strColourTable & "}\deflang2057\horzdoc{\*\fchars }{\*\lchars }\pard\plain\f" & strTableRef & "\fs" & strFontPitch & "\cf" & strTableRef & " $1}"

rtf.TextRTF = re.Replace(rtf.TextRTF, ReplacementString)



Where re is my regular expression object, rtf is the rich text box, the Replacement string is just a rich text format of "$1" so that I can define it as a certain colour and font.

The strWordList to match is just a list of words to be matched and each word must have a white space infont and after it:

strWordList = "(\s+Fox+\s|\s+Dog+\s|\s+Wizards+\s|\s+Box+\s|\s+Jugs+\s)"

I then then try and colour all characters on a line after a certain character has been found (much like commenting out code in VB). For this I use the same code but change the colour in the rtf ReplacementString and the pattern to match becomes:

strWordList = "('[\d\D]*?\n)"

Where the apostophe is the character that denotes the rest of the line being a comment.

This all works to a certain extent but I have three problems that I don't seem to be able to solve!

1) Is it possible to use "$1" so that the word in question is replaced but the first character in the word is capitalised?

2) If a word gets coloured and the word before it was previously coloured a double space is produced between them. How do I get around this?

3) If a comment line contains a word that was peviously coloured then the commenting colour stops when it reaches the word that was previously coloured. How do I get around this?

Thanks for ANY help you can give me!

elziko



yapp
User

Nov 20, 2001, 7:45 AM

Post #2 of 2 (4774 views)
Re: Colouring words [In reply to] Can't Post

I have created a color code function in Perl. I use it for my website, when I make Perl code. The script adds HTML codes, like <SPAN class="statement"> ... </SPAN> around all my statements.
All perl code in http://www.textpad.com>TextPad program does everything I want


Oeps I forgot your questions
By the way, the RTF box has some properties that allow you to color some text. i.e. use the selstart, sellegth and selcolor properties. That means the text get's selected first. (so select an other control before you do this) That's very unhandly, and I got mad of it.

Maybe this question can be posted aswell <A target="_blank" HREF=http://forums.vb-world.net/ at a VB related forum


Hope you can do something with it.



Yet an Other Perl Programmer

_________________________________
Find out more about programming
http://www.cool-programming.f2s.com

(This post was edited by yapp on Nov 20, 2001, 6:51 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives