I've been trying to find a regex pattern to replace all youtube URLs in a string with the iframe embed code (C#). Obviously the video ID has to extracted. Here is the url patterns that should match:
http://www.youtube.com/watch?v=bSiDLCf5u3s
https://www.youtube.com/watch?v=bSiDLCf5u3s
http://youtu.be/bSiDLCf5u3s
www.youtube.com/watch?v=bSiDLCf5u3s
youtu.be/bSiDLCf5u3s
http://www.youtube.com/watch?feature=player_embedded&v=bSiDLCf5u3s
www.youtube.com/watch?feature=player_embedded&v=bSiDLCf5u3s
all possible urls should be replaced with:
<iframe title='YouTube video player' width='480' height='390' src='http://www.youtube.com/embed/VIDEO_ID_EXTRACTED' frameborder='0' allowfullscreen='1'></iframe>
Can someone please point me to a right direction.
Thank you in advance
Here is the regex:
(?:https?:\/\/)?(?:www\.)?(?:(?:(?:youtube.com\/watch\?[^?]*v=|youtu.be\/)([\w\-]+))(?:[^\s?]+)?)
Should match all the links you posted and extracts the video ID as $1. And with the following code you replace the links with the <iframe/>:
const string input = "http://www.youtube.com/watch?v=bSiDLCf5u3s " +
"https://www.youtube.com/watch?v=bSiDLCf5u3s " +
"http://youtu.be/bSiDLCf5u3s " +
"www.youtube.com/watch?v=bSiDLCf5u3s " +
"youtu.be/bSiDLCf5u3s " +
"http://www.youtube.com/watch?feature=player_embedded&v=bSiDLCf5u3s " +
"www.youtube.com/watch?feature=player_embedded&v=bSiDLCf5u3s " +
"http://www.youtube.com/watch?v=_-QpUDvTdNY";
const string pattern = #"(?:https?:\/\/)?(?:www\.)?(?:(?:(?:youtube.com\/watch\?[^?]*v=|youtu.be\/)([\w\-]+))(?:[^\s?]+)?)";
const string replacement = "<iframe title='YouTube video player' width='480' height='390' src='http://www.youtube.com/embed/$1' frameborder='0' allowfullscreen='1'></iframe>";
var rgx = new Regex(pattern);
var result = rgx.Replace(input, replacement);
// result ==
// <iframe title='YouTube video player' width='480' height='390' src='https://www.youtube.com/embed/bSiDLCf5u3s' frameborder='0' allowfullscreen='1'></iframe>
// ...
//You can try this:
string strRegex = #"(?<EMBED>(<iframe|<object).*?src=[""'](?<SRC>(http:|https:)?//(www.)?[youtube\.com|youtu.be][^""']+).*?(</iframe>|</object>))";
Regex myRegex = new Regex(strRegex, RegexOptions.Singleline);
string strTargetString = #"<div align=""center""><iframe height=""315"" src=""//www.youtube.com/embed/NiCZAnmjYZ8"" frameborder=""0"" width=""560"" allowfullscreen=""true""></iframe></div> " + "\n" + #"<div align=""center""><iframe height=""315"" src=""//youtube.com/embed/NiCZAnmjYZ81"" frameborder=""0"" width=""570"" allowfullscreen=""""></iframe></div> " + "\n" + #"<div align=""center""><iframe height=""315"" src=""http://www.youtube.com/embed/NiCZAnmjYZ82"" frameborder=""0"" width=""560"" allowfullscreen=""""></iframe></div> " + "\n" + #"<div align=""center""><iframe height=""315"" src=""https://youtube.com/embed/NiCZAnmjYZ83"" frameborder=""0"" width=""560"" allowfullscreen=""""></iframe></div> " + "\n" + #"<div align=""center""><iframe height=""315"" src=""https://youtu.be/embed/NiCZAnmjYZ83"" frameborder=""0"" width=""560"" allowfullscreen=""""></iframe></div> " + "\n" + #"<div align=""center""><iframe height=""315"" src=""http://youtu.be/embed/NiCZAnmjYZ83"" frameborder=""0"" width=""560"" allowfullscreen=""""></iframe></div> " + "\n" + #"<a href=""https://youtu.be/embed/NiCZAnmjYZ83"">Youtube<a>" + "\n" + #"<div style=""text-align:center""><object width=""100%"" height=""100%"" id=""PlayerAS039128cb43804eb7894cba4e8b0220fc"" classid=""clsid:D27CDB6E-AE6D-11cf-96B8-444553540000""><param name=""movie"" value=""http://youtu.be/embed/NiCZAnmjYZ83""></param><param name=""allowFullScreen"" value=""true""></param><param name=""allowscriptaccess"" value=""always""></param><param value=""#000000"" name=""bgcolor""></param><param name=""wmode"" value=""opaque""></param><embed height=""100%"" width=""100%"" quality=""high"" bgcolor=""#000000"" flashvars="""" wmode=""opaque"" allowfullscreen=""true"" allowscriptaccess=""always"" name=""039128cb43804eb7894cba4e8b0220fc"" id=""039128cb43804eb7894cba4e8b0220fc"" style="""" src=""http://youtu.be/embed/NiCZAnmjYZ83"" type=""application/x-shockwave-flash""/></embed></object></div>" + "\n";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
// Add your code here
}
}
Related
In my application, I want to add a header and footer to a word document and download it. So I used to create HTML content and convert that content to a word document and download it. It is working on my local PC. When I hosted my application, images of the downloaded word document has not appeared.
I think images are saved in the server. when I download the document from the client-side machine, the images can't find the path. I used StreamWriter to convert HTML content to Word.
Please provide me a solution for how to bind an image with the absolute path with a permanent way to a word document through HTML or any other way to convert HTML content to word.
1st way I tried;
string renderHeaderImageLogo1 = Server.MapPath("~/Resources/ImageLogo/letter_header.jpg");
string renderFooterImageLogo1 = Server.MapPath("~/Resources/ImageLogo/letter_footer.jpg");
byte[] imageArrayHeader = System.IO.File.ReadAllBytes(renderHeaderImageLogo1);
byte[] imageArrayFooter = System.IO.File.ReadAllBytes(renderFooterImageLogo1);
string renderHeaderImageLogo = Convert.ToBase64String(imageArrayHeader);
string renderFooterImageLogo = Convert.ToBase64String(imageArrayFooter);
string headerImagePath = string.Empty;
string footerImagePath = string.Empty;
headerImagePath = "<div style=" + '"' + "text-align:" + headerImageAlignLocation + '"' + '"' + ">" + "<img src=" + "\"data:image/png;base64," + renderHeaderImageLogo + '"' + "/>" + "</div>";
footerImagePath = "<div style=" + '"' + "text-align:" + footerImageAlignLocation + '"' + '"' + ">" + "<img src=" + "\"data:image/png;base64," + renderFooterImageLogo + '"' + "/>" + "</div>";
2nd way i tried:
string renderHeaderImageLogo = Server.MapPath("~/Resources/ImageLogo/letter_header.jpg");
string renderFooterImageLogo = Server.MapPath("~/Resources/ImageLogo/letter_footer.jpg");
string headerImagePath = string.Empty;
string footerImagePath = string.Empty;
headerImagePath = "<div style=" + '"' + "text-align:" + headerImageAlignLocation + '"' + '"' + ">" + "<img src=" + '"' + renderHeaderImageLogo + '"' + "/>" + "</div>";
footerImagePath = "<div style=" + '"' + "text-align:" + footerImageAlignLocation + '"' + '"' + ">" + "<img src=" + '"' + renderFooterImageLogo + '"' + "/>" + "</div>";
3rd way I tried. This is not working for me. because I hosted my application as HTTPS
<img src="http://www.website.com/path/to/image.jpg" width="233" height="54" style="width:233pt;height:54pt;" />
This is the way I used to convert HTML content to word. content is the HTML content.
var npath = pathNew + (templateTitle.Replace(" ", "_")) + " (" + emp + "-" + empCallingName + ")" + ".doc";
try
{
using (StreamWriter writetext = new StreamWriter(npath))
{
writetext.WriteLine(content.ToString());
writetext.Close();
}
}
catch (Exception ex)
{
Log.Error(ex);
throw ex;
}
Starting with an example, I have some keywords like
Narendra Modi, Modi, India, Speech, Parliament
And I have a story with text
Narendra Modi will give a speech in the parliament of India.
Now I want my regex to replace the keyword with the hyperlink in the story.
<a>Narendra Modi</a> will give speech in the <a>parliament</a> of <a>India.</a>
My Code for this is
var tagArray = bodykeywords.Split(',').ToList();
foreach (var tag in tagArray.OrderBy(a => a.Length))
{
var replaceTag = tag.Replace("(", "");
replaceTag = replaceTag.Replace(")", "");
DataDesc = Regex.Replace(DataDesc, "\"" + replaceTag.Trim() + "\"", " \"" + replaceTag.Trim() + "\" ");
DataDesc = Regex.Replace(DataDesc, " " + replaceTag.Trim() + ", ", " " + replaceTag.Trim() + ", ");
DataDesc = Regex.Replace(DataDesc, " " + replaceTag.Trim() + " ", " " + replaceTag.Trim() + " ");
}
Problem is I am not able to replace the word with full stop like India in the given example and Word with repetition like Narendra Modi, Modi in the keyword.
Is there a way to add a pause (preferably 1 second) in Amazon Alexa without using SSML? Perhaps there is a trick I can do with the Outputspeech.Text and I just don't know it.
Below, I am saying "Here are works of art by {artist name}" but the name and the start of the works of art become mixed together - in spite of the period - so I end up with things like "Here are the works of art by Pablo Picasso Harlequin..."
I am using C# and my own https endpoint, not AWS Lambda.
Any suggestions? Otherwise I will add it as SSML. Thanks.
var output = new StringBuilder();
var outputCard = new StringBuilder();
string m_location;
string m_current_location;
string m_artist = dt_artist.Rows[0]["DisplayName"].ToString();
output.Append("here are works of art for " + m_artist + ". ");
outputCard.Append("Here are works of art for " + m_artist + ".\n\n");
foreach (DataRow dr in dt_artist_objs.Rows)
{
m_current_location = dr["CurrentLocation"].ToString();
if (m_current_location == " ")
{
m_location = "The location is not available.";
}
else
{
m_location = "It is located on the " + m_current_location;
}
output.Append(dr["Title"].ToString() + " is a " + dr["Classification"].ToString() + ". The medium is " + dr["Medium"].ToString() + ". " + m_location);
outputCard.Append(dr["Title"].ToString() + ", " + dr["Dated"].ToString() + " is a " + dr["Classification"].ToString() + ". The medium is " + dr["Medium"].ToString() + ". " + dr["Creditline"].ToString() + ". " + m_location + ".\n"); // It is located on the " + dr["CurrentLocation"].ToString());
}
sql_conn_data.Close();
response.Response.OutputSpeech.Text = output.ToString();
response.Response.Card.Title = "Art";
response.Response.Card.Type = "Standard";
response.Response.Card.Text = outputCard.ToString();
response.Response.ShouldEndSession = true;
return response;
UPDATE
OK. Ended up going the SSML route which looks like this:
var output = new StringBuilder();
var outputCard = new StringBuilder();
string m_location;
string m_current_location;
string m_location_card;
string m_artist = dt_artist.Rows[0]["DisplayName"].ToString();
output.Append("<speak>");
output.Append("here are works of art for " + m_artist + ". <break time='1s'/> ");
outputCard.Append("Here are works of art for " + m_artist + ".\n\n");
foreach (DataRow dr in dt_artist_objs.Rows)
{
m_current_location = dr["CurrentLocation"].ToString();
if (m_current_location == " ")
{
m_location = "The location is not available. <break time='1s' />";
m_location_card = "The location is not available. ";
}
else
{
m_location = "It is located on the " + m_current_location + "<break time = '1s' />";
m_location_card = "It is located on the " + m_current_location;
}
output.Append(dr["Title"].ToString() + " is a " + dr["Classification"].ToString() + ". The medium is " + dr["Medium"].ToString() + ". " + m_location);
outputCard.Append(dr["Title"].ToString() + ", " + dr["Dated"].ToString() + " is a " + dr["Classification"].ToString() + ". The medium is " + dr["Medium"].ToString() + ". " + dr["Creditline"].ToString() + ". " + m_location_card + ". \n");
}
output.Append("</speak>");
sql_conn_data.Close();
response.Response.OutputSpeech.Ssml = output.ToString();
response.Response.OutputSpeech.Type = "SSML";
response.Response.Card.Title = "Art";
response.Response.Card.Type = "Standard";
response.Response.Card.Text = outputCard.ToString();
response.Response.ShouldEndSession = true;
return response;
}
There is not a way to introduce a pause in Alexa without SSML. You will need to build the ssml string and return it back to Alexa using the pause, or the cadence strings.
Why this will print the same result?
string tester = "stUniqueId01";
Debug.WriteLine("ID: " + tester);
var regex = tester.Replace("[^0-9.]", "");
Debug.WriteLine("ID: " + regex);
Output:
ID: stUniqueId01
ID: stUniqueId01
You are calling string.Replace, not Regex.Replace. I think you want:
string tester = "stUniqueId01";
Debug.WriteLine("ID: " + tester);
var regex = new Regex("[^0-9.]");
Debug.WriteLine("ID: " + regex.Replace(tester,""));
or:
string tester = "stUniqueId01";
Debug.WriteLine("ID: " + tester);
var replaced = Regex.Replace(tester,"[^0-9.]","");
Debug.WriteLine("ID: " + replaced);
if you don't intend to reuse the regular expression.
You are using the Replace method from String. It takes strings, not regular expressions. Try:
string tester = "stUniqueId01";
Console.WriteLine("ID: " + tester);
Regex rx = new Regex("[^0-9.]");
var regex = rx.Replace(tester, "");
Console.WriteLine("ID: " + regex);
Result:
ID: stUniqueId01
ID: 01
I am trying to get a snippet of HTML between to comments.
I will need to parse the HTML between the start/end later.
I am actually reading from an html file but for test purposes I mocked the following up:
string emailFeedTxtStart = "<!--FEED FOR RECEIPT GOES HERE-->";
string emailFeedTxtEnd = "<!--FEED FOR RECEIPT ENDS HERE-->";
string html =
emailFeedTxtStart + Environment.NewLine +
#"<td align=""center"">" + Environment.NewLine +
#"<table style=""table-layout:fixed;width:380px"" border=""0"" cellspacing=""0"" cellpadding=""0"">" + Environment.NewLine +
"<tbody>" + Environment.NewLine +
"<tr>" + Environment.NewLine +
"<td>" + Environment.NewLine +
"</td>" + Environment.NewLine +
"</tr>" + Environment.NewLine +
"</tbody>" + Environment.NewLine +
"</table>" + Environment.NewLine +
"</td>" + Environment.NewLine +
emailFeedTxtEnd;
string patternstart = Regex.Escape(emailFeedTxtStart);
string patternend = Regex.Escape(emailFeedTxtEnd);
string regexexpr = patternstart + #"(.*?)" + patternend;
//string regexexpr = #"(?<=" + patternstart + ")(.*?)(?=" + patternend + ")";
MatchCollection matches = Regex.Matches(#html, #regexexpr);
matches returned is 0.
(note there is a lot more HTML between the ).
Any help would be greatly appreciated.
What are you going to parse the HTML with after? Because there's probably a way you can just do away with actually manipulating the HTML string beforehand. Here's a solution anyway:
string afterFirst = html.Substring(Regex.Match(html, emailFeedTxtStart).Index + emailFeedTxtStart.Length);
string between = afterFirst.Substring(0, Regex.Match(afterFirst, emailFeedTxtEnd).Index);