Regex in c#

N

Nightcrawler

I am building a web page where people can link in a youtube video by
copying and pasting the link that is provided by youtube. How can I
use a regular expression to capture the URL only from the link below:

<object width="425" height="355"><param name="movie" value="http://
www.youtube.com/v/xSqNx7vJLDE&rel=1"></param><param name="wmode"
value="transparent"></param><embed src="http://www.youtube.com/v/
xSqNx7vJLDE&rel=1" type="application/x-shockwave-flash"
wmode="transparent" width="425" height="355"></embed></object>

Thanks
 
N

Nicholas Paldino [.NET/C# MVP]

Personally, I wouldn't use a regular expression. I would parse this as
an XML document and then use an XPath expression ("//object/param[@value]"
is what you would use, I believe) to get the value.
 
J

Jesse Houwing

Hello Nicholas Paldino [.NET/C# MVP],

And if the copy/pasted link isn't completely XML xompliant, you can always
use the HTML Agility Pack from Codeplex.

Jesse
Personally, I wouldn't use a regular expression. I would parse
this as an XML document and then use an XPath expression
("//object/param[@value]" is what you would use, I believe) to get the
value.

I am building a web page where people can link in a youtube video by
copying and pasting the link that is provided by youtube. How can I
use a regular expression to capture the URL only from the link below:

<object width="425" height="355"><param name="movie" value="http://
www.youtube.com/v/xSqNx7vJLDE&rel=1"></param><param name="wmode"
value="transparent"></param><embed src="http://www.youtube.com/v/
xSqNx7vJLDE&rel=1" type="application/x-shockwave-flash"
wmode="transparent" width="425" height="355"></embed></object>

Thanks
 
N

Nightcrawler

Personally, I wouldn't use a regular expression. I would parse this as
an XML document and then use an XPath expression ("//object/param[@value]"
is what you would use, I believe) to get the value.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




I am building a web page where people can link in a youtube video by
copying and pasting the link that is provided by youtube. How can I
use a regular expression to capture the URL only from the link below:
<object width="425" height="355"><param name="movie" value="http://
www.youtube.com/v/xSqNx7vJLDE&rel=1"></param><param name="wmode"
value="transparent"></param><embed src="http://www.youtube.com/v/
xSqNx7vJLDE&rel=1" type="application/x-shockwave-flash"
wmode="transparent" width="425" height="355"></embed></object>
Thanks- Hide quoted text -

- Show quoted text -

Well, I also wanted to use the regular expression in a
regularexpressionvalidator on the page to ensure it is a youtube
linking url. Once it passes the validator I would use the same regex
to extract the URL in the backend and store it to the database. So a
regular expression is the easiest for me.

Thanks
 
B

Brian Muth

Regex r = new Regex ("src=\"(?<YouTube>http://www\\.youtube\\.com\\S*)\"");
Match s = r.Match("<object width=\"425\" height=\"355\"><param name=\"movie\"
value=\"
"></param><param name=\"wmode\"value=\"transparent\"></param><embed
src=\"
" type=\"application/x-shockwave-flash\"wmode=\"transparent\" width=\"425\"
height=\"355\"></embed></object>");

Console.Write(s.Groups["YouTube"]);

Watch out for unintended line-breaks.

Brian
 
N

Nightcrawler

Regex r = new Regex ("src=\"(?<YouTube>http://www\\.youtube\\.com\\S*)\"");
Match s = r.Match("<object width=\"425\" height=\"355\"><param name=\"movie\"
value=\"
"></param><param name=\"wmode\"value=\"transparent\"></param><embed
src=\"
" type=\"application/x-shockwave-flash\"wmode=\"transparent\" width=\"425\"
height=\"355\"></embed></object>");

Console.Write(s.Groups["YouTube"]);

Watch out for unintended line-breaks.

Brian

Brian,

Thanks! How do I use this in a regularexpression validator in my aspx
file. There is a problem using quotation marks within the
ValidationExpression="" property.

Thanks
 
J

Jesse Houwing

Hello Nightcrawler,
Regex r = new Regex
("src=\"(?<YouTube>http://www\\.youtube\\.com\\S*)\"");

Match s = r.Match("<object width=\"425\" height=\"355\"><param
name=\"movie\"

value=\"
"></param><param
name=\"wmode\"value=\"transparent\"></param><embed

src=\"
"
type=\"application/x-shockwave-flash\"wmode=\"transparent\"
width=\"425\"

height=\"355\"></embed></object>");

Console.Write(s.Groups["YouTube"]);

Watch out for unintended line-breaks.

Brian
Brian,

Thanks! How do I use this in a regularexpression validator in my aspx
file. There is a problem using quotation marks within the
ValidationExpression="" property.

Write them as &quot; I guess... Or set the value through the designer.

Jesse
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top