472,785 Members | 1,234 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,785 software developers and data experts.

Regex Pattern Matching algorithm in mono/c#

Jeff_Relf wrote:
...yet you don't even know what RegEx is.


I'm looking at the source code for mono's Regex implementation right
now. You can download that source here ( use the class libraries
download ).

http://www.mono-project.com/Downloads

One of the files ( quicksearch.cs -- it's all written in mono as well )
says it uses "simplified Boyer-Moore" for fast substring matching.
That is the method I learned in CS ( using the SNOBOL compiler ).

Here's a page that demonstrates, step-by-step, text matching algorithms,
including Boyer-Moore.

http://www-sr.informatik.uni-tuebing...ler/BM/BM.html

Here's that source for quicksearch w/in the mono regex...interesting
stuff...

//
// assembly: System
// namespace: System.Text.RegularExpressions
// file: quicksearch.cs
//
// Authors: Dan Lewis (dl****@gmx.co.uk)
// Juraj Skripsky (ju***@hotfeet.ch)
//
// (c) 2002 Dan Lewis
// (c) 2003 Juraj Skripsky
//

//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections;

namespace System.Text.RegularExpressions {
internal class QuickSearch {
// simplified boyer-moore for fast substring matching
// (for short strings, we use simple scans)
public QuickSearch (string str, bool ignore)
: this(str, ignore, false)
{
}

public QuickSearch (string str, bool ignore, bool reverse) {
this.str = str;
this.len = str.Length;
this.ignore = ignore;
this.reverse = reverse;

if (ignore)
str = str.ToLower ();

// create the shift table only for "long" search strings
if(len > THRESHOLD)
SetupShiftTable ();
}

public string String {
get { return str; }
}

public int Length {
get { return len; }
}

public bool IgnoreCase {
get { return ignore; }
}

public int Search (string text, int start, int end) {
int ptr = start;
if ( reverse )
{
if (start < end)
return -1;

if ( ptr > text.Length)
{
ptr = text.Length;
}

// use simple scan for a single-character search string
if (len == 1)
{
while (--ptr >= end)
{
if(str[0] == GetChar(text[ptr]))
return ptr ;

}
return -1;
}
if ( end < len)
end = len - 1 ;

ptr--;
while (ptr >= end)
{
int i = len -1 ;
while (str[i] == GetChar(text[ptr - len +1 + i]))
{
if (-- i < 0)
return ptr - len + 1;
}

if (ptr > end)
{
ptr -= GetShiftDistance (text[ptr - len ]);

}
else
break;
}

}
else
{
// use simple scan for a single-character search string
if (len == 1)
{
while (ptr <= end)
{
if(str[0] == GetChar(text[ptr]))
return ptr;
else
ptr++;
}
return -1;
}

if (end > text.Length - len)
end = text.Length - len;

while (ptr <= end)
{
int i = len - 1;
while (str[i] == GetChar(text[ptr + i]))
{
if (-- i < 0)
return ptr;
}

if (ptr < end)
ptr += GetShiftDistance (text[ptr + len]);
else
break;
}
}

return -1;

}

// private

private void SetupShiftTable () {
shift = new Hashtable ();
if (reverse)
{
for (int i = len ; i > 0; -- i)
{
char c = str[i -1];
shift[GetChar(c)] = i;
}
}
else
{
for (int i = 0; i < len; ++ i)
{
char c = str[i];
shift[GetChar(c)] = len - i;
}
}

}

private int GetShiftDistance (char c) {
if(shift == null)
return 1;

object s = shift [GetChar (c)];
return (s != null ? (int)s : len + 1);
}

private char GetChar(char c) {
return (!ignore ? c : Char.ToLower(c));
}

private string str;
private int len;
private bool ignore;
private bool reverse;

private Hashtable shift;
private readonly static int THRESHOLD = 5;
}

}
Jul 21 '05 #1
3 2679

Hi John, Re: Your Total lack of RegEx knowledge,
Ya gone done an' writ: << I'm looking at the source code
for mono's Regex implementation right now. >>
<< Here's a page that demonstrates, step-by-step,
text matching algorithms, including Boyer-Moore. >>
<< Here's that source for quicksearch w/in the mono regex
...interesting stuff... >>

Anyone can glance at code... How long did that take you ? 20 seconds ?
The difference is that I can Understand it.
Glancing at that code did not suddenly transform you into a RegEx guru.

Jul 21 '05 #2
Jeff_Relf wrote:
Anyone can glance at code... How long did that take you ? 20 seconds ?
Long enough to recognized that they used an efficient algorithm.
The difference is that I can Understand it.
Great...now you know how bad your code is.
Glancing at that code did not suddenly transform you into a RegEx guru.


With OSS, it does...everyone can see the code...it's brilliance, and its
flaws...
Jul 21 '05 #3

Hi John,
Re: How glancing at a RegEx implementation doesn't make one a guru,

Ya told me: << With OSS, it does
...everyone can see the code...it's brilliance, and its flaws. >>

Theoretically yes, if enough time/money were invested,
but not in practice, as you so aptly demonstrate.

Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: bpontius | last post by:
The GES Algorithm A Surprisingly Simple Algorithm for Parallel Pattern Matching "Partially because the best algorithms presented in the literature are difficult to understand and to implement,...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
7
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning...
2
by: Mortimer Schnurd | last post by:
Hi All, I am a VB 6 programmer who is now trying to learn C#. In doing so, I am trying to convert some of my VB modules to C#. I routinely user Reg Expressions in VB and am having some trouble...
2
by: Steve | last post by:
Hi, I have an array of strings e.g. "one","two","three","four","fourteen" From the following string "onethreefourteenten" I would like to know that strings have been matched; strings ,, but...
7
by: bill tie | last post by:
I'd appreciate it if you could advise. 1. How do I replace "\" (backslash) with anything? 2. Suppose I want to replace (a) every occurrence of characters "a", "b", "c", "d" with "x", (b)...
3
by: Day Of The Eagle | last post by:
Jeff_Relf wrote: > ...yet you don't even know what RegEx is. > I'm looking at the source code for mono's Regex implementation right now. You can download that source here ( use the class...
8
by: Bob | last post by:
I need to create a Regex to extract all strings (including quotations) from a C# or C++ source file. After being unsuccessful myself, I found this sample on the internet: ...
11
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.