469,963 Members | 2,188 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,963 developers. It's quick & easy.

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 2406
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Fortepianissimo | last post: by
1 post views Thread by Mark | last post: by
4 posts views Thread by sasifiqbal | last post: by
reply views Thread by Tanja Krammer | last post: by
reply views Thread by Jorge O. Varona | last post: by
5 posts views Thread by =?Utf-8?B?SkF1bA==?= | last post: by
4 posts views Thread by Danny Ni | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.