4 Replies - 1058 Views - Last Post: 08 May 2009 - 06:05 AM Rate Topic: -----

#1 Latheesan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-January 09

Quick RegEx Question

Posted 06 May 2009 - 05:26 PM

Hello,

I came across this syntax highlighting c# tutorial and i decided to write my own version for one of my project.

This is the method responsible for highlighting the syntax in a given line (from rtb)

		// Process This Line Of Text
		private void ProcessLine()
		{
			// Add Keywords - TODO
			_keyword_list.Add("xml");

			// Set Color Parameter - TODO
			_keyword_color = Color.Red;

			// Compile RegEx String
			string _temp_keyword_list = "";
			for (int i = 0; i < _keyword_list.Count; i++)
			{
				// Update _temp_keyword_list String
				_temp_keyword_list += "\\b" + _keyword_list[i] + "\\b";
				if (i != (_keyword_list.Count - 1))
				{
					_temp_keyword_list += "|";
				}
			}

			// Save Position & Make Curent Line's Text Black Color
			int _position = Selectionstart;
			Selectionstart = _line_start;
			SelectionLength = _line_length;
			SelectionColor = Color.Black;

			// Create A RegEx Object
			Regex _reg_keywords = new Regex(_temp_keyword_list, RegexOptions.IgnoreCase | RegexOptions.Compiled);
			Match _reg_match;

			// Process Every Matched Words
			for (_reg_match = _reg_keywords.Match(_current_line); _reg_match.Success; _reg_match = _reg_match.NextMatch())
			{
				int _temp_start = _line_start + _reg_match.Index;
				int _temp_length = _reg_match.Length;
				Selectionstart = _temp_start;
				SelectionLength = _temp_length;
				SelectionColor = _keyword_color;
			}

			// Get & Set Updated Data
			Selectionstart = _position;
			SelectionLength = 0;
			SelectionColor = Color.Black;
			_current_selection = _position;
		}


When i run my test application, the syntax highlighting works fine like this -> http://img27.imagesh...96/91309633.png

The thing is, syntax highlighting only works if there are spaces between my keywords. Take a look at this -> http://i40.tinypic.com/2n176ep.png

see what i mean?

My question was, is it possible to highlight a keyword regardless what's in front or back of the matched text?

for example:

text on my rtb = abcd123

one of my keyword = cd1

when the regex is matched, the text should look like this in the rtb : abcd123

This post has been edited by Latheesan: 07 May 2009 - 12:39 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Quick RegEx Question

#2 SixOfEleven   User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: Quick RegEx Question

Posted 06 May 2009 - 06:37 PM

As far as I can tell your program only checks for whole words. If you wanted it to check inside each word you would want to update it a little. You would have to find a way to search through each word and find out if the selection inside the word matches.

*edit*
That could have some undesirable effects. For example, if you type ( c ) without spaces in this editor you get ©. Not always desirable.

This post has been edited by SixOfEleven: 06 May 2009 - 06:39 PM

Was This Post Helpful? 0
  • +
  • -

#3 Latheesan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-January 09

Re: Quick RegEx Question

Posted 06 May 2009 - 06:58 PM

I have this idea for checking char but i need a little help.

what i am working on is a custom RTB control right? how do you get the whole text of the current line inside the ProcessLine() method?

For example http://img27.imagesh...96/91309633.png, i want to retrieve the text "bla bla xml bla bla" (which is the current line im editing) inisde the ProcessLine() method. If i can get this, i think i know how to solve this problem.
Was This Post Helpful? 0
  • +
  • -

#4 Latheesan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-January 09

Re: Quick RegEx Question

Posted 06 May 2009 - 08:52 PM

I found a way to extract the text from current line that is being processed.

This is my attempted implementation ...

		// Process This Line Of Text
		private void ProcessLine()
		{
			// Add Keywords
			_keyword_list.Add("xml");

			// Add Chars
			_char_list.Add('<');

			// Set Color Parameter
			_keyword_color = Color.Red;
			_char_color = Color.Red;
						
			// Compile RegEx Pattern
			string _temp_keyword_list = "";
			for (int i = 0; i < _keyword_list.Count; i++)
			{
				// Update _temp_keyword_list String
				_temp_keyword_list += "\\b" + _keyword_list[i].ToString() + "\\b";
				if (i != (_keyword_list.Count - 1))
				{
					_temp_keyword_list += "|";
				}
			}

			// Save Position & Make Curent Line's Text Black Color
			int _position = Selectionstart;
			Selectionstart = _line_start;
			SelectionLength = _line_length;
			SelectionColor = Color.Black;

			// Process Every Char First
			for (int i = 0; i < _char_list.Count; i++)
			{
				// Check If This Line Text Contains Any Matching Chars
				if (_current_line.IndexOf(_char_list[i]) != -1)
				{
					// Convert The Entire Current Line String To Char Array
					char[] _current_line_chars = _current_line.ToCharArray();
					for (int j = 0; j < _current_line_chars.Length; j++)
					{
						if (_current_line_chars[j] == _char_list[i])
						{
							// Color this Char
							int _temp_start = _line_start + j;
							int _temp_length = 1;
							Selectionstart = _temp_start;
							SelectionLength = _temp_length;
							SelectionColor = _char_color;
						}
					}
				}
			}

			// Get & Set Updated Data
			Selectionstart = _position;
			SelectionLength = 0;
			SelectionColor = Color.Black;
			_current_selection = _position;

			// Save Position & Make Curent Line's Text Black Color
			_position = Selectionstart;
			Selectionstart = _line_start;
			SelectionLength = _line_length;
			SelectionColor = Color.Black;

			// Create A RegEx Keyword Object
			Regex _reg_keywords = new Regex(_temp_keyword_list, RegexOptions.IgnoreCase | RegexOptions.Compiled);
			Match _reg_keywords_match;

			// Process Every Matched Words
			for (_reg_keywords_match = _reg_keywords.Match(_current_line); _reg_keywords_match.Success; _reg_keywords_match = _reg_keywords_match.NextMatch())
			{
				// Color This Keyword
				int _temp_start = _line_start + _reg_keywords_match.Index;
				int _temp_length = _reg_keywords_match.Length;
				Selectionstart = _temp_start;
				SelectionLength = _temp_length;
				SelectionColor = _keyword_color;
			}

			// Get & Set Updated Data
			Selectionstart = _position;
			SelectionLength = 0;
			SelectionColor = Color.Black;
			_current_selection = _position;
		}


trial and error -> http://i43.tinypic.com/2cxvryb.png

when ever i type the keyword it gets highlighted fine, but when i enter < it doesnt get highlighted red like it should.

so any idea what might be wrong? This is the complete source code if you interested in helping me, thanks in advance -> http://pastebin.ca/1415079

This post has been edited by Latheesan: 06 May 2009 - 11:27 PM

Was This Post Helpful? 0
  • +
  • -

#5 Latheesan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-January 09

Re: Quick RegEx Question

Posted 08 May 2009 - 06:05 AM

- bump -

anyone?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1