473,226 Members | 1,391 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,226 software developers and data experts.

Need help with Regex

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
Regex(@"<embed(\s+[a-z]+\s*=\s*(""[^""]*""|'[^']*'|[^\s]*))*\s+src=\s*(""|')?http://www.g4tv.com/i?sv3?/(?<videokey>\d+)(""|')?(\s+[a-z]+\s*=\s*(""[^""]*""|'[^']*'|[^\s]*))*\s*(/\s*>|>\s*</embed>)",
RegexOptions.IgnoreCase);
string strBody = "<embed name=\"VideoPlayer\"
src=\"http://localhost/lv3/26757\" width=\"480\" height=\"418\"
scale=\"ShowAll\" loop=\"loop\" menu=\"menu\" wmode=\"Window\" quality=\"1\"
type=\"application/x-shockwave-flash\"></embed>" +
"<embed name=\"VideoPlayer\" src=\"http://localhost/lv3/19251\"
width=\"480\" height=\"418\" scale=\"ShowAll\" loop=\"loop\" menu=\"menu\"
wmode=\"Window\" quality=\"1\"
type=\"application/x-shockwave-flash\"></embed>" +
"<embed name=\"VideoPlayer\" src=\"http://localhost/lv3/20202\"
width=\"480\" height=\"418\" scale=\"ShowAll\" loop=\"loop\" menu=\"menu\"
wmode=\"Window\" quality=\"1\"
type=\"application/x-shockwave-flash\"></embed>" +
"<embed name=\"VideoPlayer\" src=\"http://localhost/lv3/16549\"
width=\"480\" height=\"418\" scale=\"ShowAll\" loop=\"loop\" menu=\"menu\"
wmode=\"Window\" quality=\"1\"
type=\"application/x-shockwave-flash\"></embed>";
foreach (Match objMatch in rgxVideo.Matches(strBody)) // loop
indefinitely here
{
}

TIA

Jul 31 '08 #1
4 2264
Hello Danny,

I've found Expresso doesn't work well enough for .Net regex. Use the regex
designer at http://www.radsoftware.com.au/ it's free. Check it with that.
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
Regex(@"<embed(\s+[a-z]+\s*=\s*(""[^""]*""|'[^']*'|[^\s]*))*\s+src=\s*
(""|')?http://www.g4tv.com/i?sv3?/(?<videokey>\d+)(""|')?(\s+[a-z]+\s*
=\s*(""[^""]*""|'[^']*'|[^\s]*))*\s*(/\s*>|>\s*</embed>)",
RegexOptions.IgnoreCase);
string strBody = "<embed name=\"VideoPlayer\"
src=\"http://localhost/lv3/26757\" width=\"480\" height=\"418\"
scale=\"ShowAll\" loop=\"loop\" menu=\"menu\" wmode=\"Window\"
quality=\"1\"
type=\"application/x-shockwave-flash\"></embed>" +
"<embed name=\"VideoPlayer\"
src=\"http://localhost/lv3/19251\"
width=\"480\" height=\"418\" scale=\"ShowAll\" loop=\"loop\"
menu=\"menu\"
wmode=\"Window\" quality=\"1\"
type=\"application/x-shockwave-flash\"></embed>" +
"<embed name=\"VideoPlayer\"
src=\"http://localhost/lv3/20202\"
width=\"480\" height=\"418\" scale=\"ShowAll\" loop=\"loop\"
menu=\"menu\"
wmode=\"Window\" quality=\"1\"
type=\"application/x-shockwave-flash\"></embed>" +
"<embed name=\"VideoPlayer\"
src=\"http://localhost/lv3/16549\"
width=\"480\" height=\"418\" scale=\"ShowAll\" loop=\"loop\"
menu=\"menu\"
wmode=\"Window\" quality=\"1\"
type=\"application/x-shockwave-flash\"></embed>";
foreach (Match objMatch in rgxVideo.Matches(strBody)) //
loop
indefinitely here
{
}

TIA

Jul 31 '08 #2
Hello Danny,

I've found Expresso doesn't work well enough for .Net regex. Use the regex
designer at http://www.radsoftware.com.au/ it's free. Check it with that.
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
Regex(@"<embed(\s+[a-z]+\s*=\s*(""[^""]*""|'[^']*'|[^\s]*))*\s+src=\s*
(""|')?http://www.g4tv.com/i?sv3?/(?<videokey>\d+)(""|')?(\s+[a-z]+\s*
=\s*(""[^""]*""|'[^']*'|[^\s]*))*\s*(/\s*>|>\s*</embed>)",
RegexOptions.IgnoreCase);
string strBody = "<embed name=\"VideoPlayer\"
src=\"http://localhost/lv3/26757\" width=\"480\" height=\"418\"
scale=\"ShowAll\" loop=\"loop\" menu=\"menu\" wmode=\"Window\"
quality=\"1\"
type=\"application/x-shockwave-flash\"></embed>" +
"<embed name=\"VideoPlayer\"
src=\"http://localhost/lv3/19251\"
width=\"480\" height=\"418\" scale=\"ShowAll\" loop=\"loop\"
menu=\"menu\"
wmode=\"Window\" quality=\"1\"
type=\"application/x-shockwave-flash\"></embed>" +
"<embed name=\"VideoPlayer\"
src=\"http://localhost/lv3/20202\"
width=\"480\" height=\"418\" scale=\"ShowAll\" loop=\"loop\"
menu=\"menu\"
wmode=\"Window\" quality=\"1\"
type=\"application/x-shockwave-flash\"></embed>" +
"<embed name=\"VideoPlayer\"
src=\"http://localhost/lv3/16549\"
width=\"480\" height=\"418\" scale=\"ShowAll\" loop=\"loop\"
menu=\"menu\"
wmode=\"Window\" quality=\"1\"
type=\"application/x-shockwave-flash\"></embed>";
foreach (Match objMatch in rgxVideo.Matches(strBody)) //
loop
indefinitely here
{
}

TIA

Jul 31 '08 #3
Danny,

I tried this in Expresso and it predicts the same behavior you should see in
code, namely that the execution time of your regex grows exponentially with
the size of the input string. I'm guessing that when you tested it in
Expresso, you used a shorter input string or one that easily found a match,
thereofe it terminated quickly. The example in your code does not have a
match (for example, "g4tv" will never match). The regex engine has to try
every possible permutation of your regex hunting for a match. The number of
permutations grows exponentially with the size of the string, so your
application hangs, while it continues to try new permutations. There are
dangerous things in your regex design that cause this. Be very careful with
nested quantifiers, especially when applied to wildcards, like (.*)*. Things
like this can cause the execution time to double every time a single
character is added to the input text. It may work fine for 100 characters,
but add 10 more and the execution time goes up by a factor of 1000, or add 20
characters (a 20% increase in length) and the times goes up by one million
times.

JWT

P.S. I don't know what the Colorado Kid is talking about. Expresso is
specifically designed to work with .NET regex.

"Danny Ni" wrote:
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
Regex(@"<embed(\s+[a-z]+\s*=\s*(""[^""]*""|'[^']*'|[^\s]*))*\s+src=\s*(""|')?http://www.g4tv.com/i?sv3?/(?<videokey>\d+)(""|')?(\s+[a-z]+\s*=\s*(""[^""]*""|'[^']*'|[^\s]*))*\s*(/\s*>|>\s*</embed>)",
RegexOptions.IgnoreCase);
string strBody = "<embed name=\"VideoPlayer\"
src=\"http://localhost/lv3/26757\" width=\"480\" height=\"418\"
scale=\"ShowAll\" loop=\"loop\" menu=\"menu\" wmode=\"Window\" quality=\"1\"
type=\"application/x-shockwave-flash\"></embed>" +
"<embed name=\"VideoPlayer\" src=\"http://localhost/lv3/19251\"
width=\"480\" height=\"418\" scale=\"ShowAll\" loop=\"loop\" menu=\"menu\"
wmode=\"Window\" quality=\"1\"
type=\"application/x-shockwave-flash\"></embed>" +
"<embed name=\"VideoPlayer\" src=\"http://localhost/lv3/20202\"
width=\"480\" height=\"418\" scale=\"ShowAll\" loop=\"loop\" menu=\"menu\"
wmode=\"Window\" quality=\"1\"
type=\"application/x-shockwave-flash\"></embed>" +
"<embed name=\"VideoPlayer\" src=\"http://localhost/lv3/16549\"
width=\"480\" height=\"418\" scale=\"ShowAll\" loop=\"loop\" menu=\"menu\"
wmode=\"Window\" quality=\"1\"
type=\"application/x-shockwave-flash\"></embed>";
foreach (Match objMatch in rgxVideo.Matches(strBody)) // loop
indefinitely here
{
}

TIA

Aug 1 '08 #4
Kottekoe,

I used to use Expresso for regex testing in my .Net programs, but one day,
something worked in Expresso, but didn't in actual .Net so I ditched it for
the Rad Regex Designer, which is a great tool. I liked Expresso, but find
Rad's better.
Danny,

I tried this in Expresso and it predicts the same behavior you should
see in code, namely that the execution time of your regex grows
exponentially with the size of the input string. I'm guessing that
when you tested it in Expresso, you used a shorter input string or one
that easily found a match, thereofe it terminated quickly. The example
in your code does not have a match (for example, "g4tv" will never
match). The regex engine has to try every possible permutation of your
regex hunting for a match. The number of permutations grows
exponentially with the size of the string, so your application hangs,
while it continues to try new permutations. There are dangerous things
in your regex design that cause this. Be very careful with nested
quantifiers, especially when applied to wildcards, like (.*)*. Things
like this can cause the execution time to double every time a single
character is added to the input text. It may work fine for 100
characters, but add 10 more and the execution time goes up by a factor
of 1000, or add 20 characters (a 20% increase in length) and the times
goes up by one million times.

JWT

P.S. I don't know what the Colorado Kid is talking about. Expresso is
specifically designed to work with .NET regex.

"Danny Ni" wrote:
>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

Regex(@"<embed(\s+[a-z]+\s*=\s*(""[^""]*""|'[^']*'|[^\s]*))*\s+src=\s
*(""|')?http://www.g4tv.com/i?sv3?/(?<videokey>\d+)(""|')?(\s+[a-z]+\
s*=\s*(""[^""]*""|'[^']*'|[^\s]*))*\s*(/\s*>|>\s*</embed>)",

RegexOptions.IgnoreCase);

string strBody = "<embed name=\"VideoPlayer\"

src=\"http://localhost/lv3/26757\" width=\"480\" height=\"418\"

scale=\"ShowAll\" loop=\"loop\" menu=\"menu\" wmode=\"Window\"
quality=\"1\"

type=\"application/x-shockwave-flash\"></embed>" +

"<embed name=\"VideoPlayer\" src=\"http://localhost/lv3/19251\"

width=\"480\" height=\"418\" scale=\"ShowAll\" loop=\"loop\"
menu=\"menu\"

wmode=\"Window\" quality=\"1\"

type=\"application/x-shockwave-flash\"></embed>" +

"<embed name=\"VideoPlayer\" src=\"http://localhost/lv3/20202\"

width=\"480\" height=\"418\" scale=\"ShowAll\" loop=\"loop\"
menu=\"menu\"

wmode=\"Window\" quality=\"1\"

type=\"application/x-shockwave-flash\"></embed>" +

"<embed name=\"VideoPlayer\" src=\"http://localhost/lv3/16549\"

width=\"480\" height=\"418\" scale=\"ShowAll\" loop=\"loop\"
menu=\"menu\"

wmode=\"Window\" quality=\"1\"

type=\"application/x-shockwave-flash\"></embed>";

foreach (Match objMatch in rgxVideo.Matches(strBody)) // loop

indefinitely here

{

}

TIA

Aug 1 '08 #5

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

Similar topics

3
by: Joe | last post by:
Hi, I have been using a regular expression that I don’t uite understand to filter the valid email address. My regular expression is as follows: <asp:RegularExpressionValidator...
2
by: Luhar | last post by:
After much scouring of information on Regular Expressions from books and the web, I've come up with the this handy little Regex to parse links from HTML: ...
2
by: Michael R. Pierotti | last post by:
Dim reg As New Regex("^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$") Dim m As Match = reg.Match(txtIPAddress.Text) If m.Success Then 'No need to do anything here Else MessageBox.Show("You need to enter a...
1
by: hillcountry74 | last post by:
Hi, I'm stuck with this regular expression from past 2 days. Desperately need help. I need a regular expression that will allow all characters except these *:~<>' This is my code in...
9
by: jmchadha | last post by:
I have got the following html: "something in html ... etc.. city1... etc... <a class="font1" href="city1.html" onclick="etc."click for <b>info</bon city1 </a> ... some html. city1.. can repeat...
7
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
3
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
I'm trying to learn regex but since I've spent way too much time on the following "simple" case, there's obviously something I'm missing. I need to find all occurrences of a specific...
8
by: Alexander Vasilevsky | last post by:
This code Regex ge = new Regex(""); string format = ge.Replace("8 kBit/s, 8,000 Hz, Mono", "_"); returns "8 kBit_s_ 8_000 Hz_ Mono" and I need "8_kBit_s_ 8_000_Hz_ Mono"
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.