473,386 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Regex hangs

I have a regex for matching VB6-functions, but it hangs on one
specific function.
The regex:

^(Public\s)?(Declare\s|Static\s)?(Function)\s(\S)+ \(((Optional\s)?(ByVal\s|ByRef\s)?(ParamArray\s)?( \S)+(\sAs\s(\S)+)?(\s=\s(\S)+)?(\,\s)?)*\)(\sAs\s( \S)+)?

The string on which it hangs:

"Public Function FlaggAsOccupiedEx2(Pid As Long, UserName As String,
Workstation As String, Optional IsReadOnly As Boolean = False,
Optional Path As String = "", Optional Reason As String = "No
reason...") As Boolean"

A similar string, but lacking the last parameter, does not fail:

"Public Function FlaggAsOccupiedEx(Pid As Long, UserName As String,
Workstation As String, Optional IsReadOnly As Boolean = False,
Optional Path As String = "") As Boolean"

Any ideas? Is this a bug?
Nov 16 '05 #1
3 2660
probably more likely that yo've got a circular backreference thingy in there or something, can't be arsed to analyse it though. Probably something to do with "lookbehind" (whatever that does).

"Vidar Skjelanger" wrote:
I have a regex for matching VB6-functions, but it hangs on one
specific function.
The regex:

^(Public\s)?(Declare\s|Static\s)?(Function)\s(\S)+ \(((Optional\s)?(ByVal\s|ByRef\s)?(ParamArray\s)?( \S)+(\sAs\s(\S)+)?(\s=\s(\S)+)?(\,\s)?)*\)(\sAs\s( \S)+)?

The string on which it hangs:

"Public Function FlaggAsOccupiedEx2(Pid As Long, UserName As String,
Workstation As String, Optional IsReadOnly As Boolean = False,
Optional Path As String = "", Optional Reason As String = "No
reason...") As Boolean"

A similar string, but lacking the last parameter, does not fail:

"Public Function FlaggAsOccupiedEx(Pid As Long, UserName As String,
Workstation As String, Optional IsReadOnly As Boolean = False,
Optional Path As String = "") As Boolean"

Any ideas? Is this a bug?

Nov 16 '05 #2
Hi,
inline

"Vidar Skjelanger" <vi***@unimicro.no> wrote in message
news:f5**************************@posting.google.c om...
I have a regex for matching VB6-functions, but it hangs on one
specific function.
The regex:

^(Public\s)?(Declare\s|Static\s)?(Function)\s(\S)+ \(((Optional\s)?(ByVal\s|B
yRef\s)?(ParamArray\s)?(\S)+(\sAs\s(\S)+)?(\s=\s(\ S)+)?(\,\s)?)*\)(\sAs\s(\S
)+)?

Probely you don't want to use (\S)+ which captures a lot of characters,
instead of (\S+) which may capture words.
For "parameter name" and "parameter type" you want anything except space or
comma. [^\s,]
For optional value you do want spaces, but you don't want comma or ')'.
[^,\)]

With some minor changes the regex works again:

^(Public\s)?(Declare\s|Static\s)?(Function)\s(\S+) \(((Optional\s)?(ByVal\s|B
yRef\s)?(ParamArray\s)?([^\s,]+)(\sAs\s([^\s,]+))?(\s=\s([^,\)]+))?(\,\s)?)*
\)(\sAs\s(\S+))?
Depending on what you want to do it may not be usefull, because you can't
tell which optional value belongs to what parameter.
If you need better you can perform it in two fases, first get the
parameter-string, then use Regex.Matches to parse the parameter-string into
parameters, this way each parameter has it's own Match :

Match m = Regex.Match(strInput,
@"(?<public>Public\s)?(?<ds>Declare\s|Static\s)?Fu nction\s(?<ftn>\S+)\((?<pa
rams>[^\)]*)\)(\sAs\s(?<ret>\S+)), RegexOptions.ExplicitCapture);

Console.WriteLine("Name={0} public={1} ds={2} ret-type={3}",
m.Groups["ftn"].Value,
m.Groups["public"].Success,
m.Groups["ds"].Value,
m.Groups["ret"].Value);

// parse parameters
MatchCollection mc = Regex.Matches(m.Groups["params"].Value,
@"(?<opt>Optional\s)?(?<mod>ByVal\s|ByRef\s)?(?<pa >ParamArray\s)?(?<name>[^\
s,]+)(\sAs\s(?<type>[^\s,]+))?(\s=\s(?<optv>[^,]+))?(,\s)?",
RegexOptions.ExplicitCapture);

foreach (Match p in mc)
{
Console.WriteLine("\tName={0} Modifier={1} ParamArray={2} Type={3}
Optional={4} OptionalValue={5}",
p.Groups["name"].Value,
p.Groups["mod"].Value,
p.Groups["pa"].Success,
p.Groups["type"].Value,
p.Groups["opt"].Success,
p.Groups["optv"].Value);
}
HTH,
greetings


The string on which it hangs:

"Public Function FlaggAsOccupiedEx2(Pid As Long, UserName As String,
Workstation As String, Optional IsReadOnly As Boolean = False,
Optional Path As String = "", Optional Reason As String = "No
reason...") As Boolean"

A similar string, but lacking the last parameter, does not fail:

"Public Function FlaggAsOccupiedEx(Pid As Long, UserName As String,
Workstation As String, Optional IsReadOnly As Boolean = False,
Optional Path As String = "") As Boolean"

Any ideas? Is this a bug?

Nov 16 '05 #3
"BMermuys" <so*****@someone.com> wrote in message news:<uH**************@TK2MSFTNGP10.phx.gbl>...
Probely you don't want to use (\S)+ which captures a lot of characters,
instead of (\S+) which may capture words.
For "parameter name" and "parameter type" you want anything except space or
comma. [^\s,]
For optional value you do want spaces, but you don't want comma or ')'.
[^,\)]

With some minor changes the regex works again:

^(Public\s)?(Declare\s|Static\s)?(Function)\s(\S+) \(((Optional\s)?(ByVal\s|B
yRef\s)?(ParamArray\s)?([^\s,]+)(\sAs\s([^\s,]+))?(\s=\s([^,\)]+))?(\,\s)?)*
\)(\sAs\s(\S+))?


It works! Thank you! :)
Nov 16 '05 #4

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

Similar topics

2
by: Fortepianissimo | last post by:
Could someone explains why the following code hangs (Python 2.3.3)? --- CODE STARTS --- import re p=re.compile(r'\S+(?:\.\S+)+\.com') t='......................................' p.search(t)...
1
by: Mark | last post by:
Hi, I've seen some postings on this but not exactly relating to this posting. I'm reading in a large mail message as a string. In the string is an xml attachment that I need to parse out and...
4
by: sasifiqbal | last post by:
Hi All, I have following text Template that needs to be parsed using Regular Expression -- Test Template This is a Test Template for <#OrderNumber/> Sender Message is <#SENDERMESSAGE/>
1
by: jason | last post by:
I have some asp.net (vb) code that is being fed some variables. Sometimes, the regex pattern can't be found the process hangs and start eating up server cpu (at 99%) for several minutes until it...
0
by: Tanja Krammer | last post by:
Hi Experts, why the following simple regex hangs? (Regex(@"(*)""((?:\\.|*)*)""", RegexOptions.Multiline); does not hangs and works fine) Thank you a lot
0
by: Jorge O. Varona | last post by:
Greetings, I have a Regex that hangs when a particular portion of text runs. Anyone have any insight as to why this particalar block of text kills my Regex? Regex =...
5
by: =?Utf-8?B?SkF1bA==?= | last post by:
I am currently working on a project and need to get a return… even if that return is a failure. I must also add that I have no control over either the Regular Expression that will be used or the...
2
by: Tomislav Fistric | last post by:
Hi all. I have very uncommon problem. I am running IIS 6.0 on 2xXEON dual core 3.06 (8 virtual CPUs) with 4 gb of Ecc ram and i get system.outofmemory exception on asp.net website. Thing is...
4
by: Danny Ni | last post by:
Hi, The following code snippet is causing CPU to max out on my local machine and production servers. It looks fine on Expresso though. Regex rgxVideo = new...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.