I think it is not very convenient for an user the get this link here:
https://www.youtube.com/embed/GmvM6syadl0
Because YouTube provides an entire code snipped like so:
<iframe width="560" height="315" src="https://www.youtube.com/embed/GmvM6syadl0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
It would be a lot better if the user could take the code snippet above and my program is just going to extract the url for him.
Any ideas how to go about this? I'm usually not very good at extracting data from elaborate strings, what I would like to end up with is something like this:
let yTLink = extractYoutubeLinkfromIframe(providedInput);
extractYoutubeLinkfromIframe(iframeTag) {
// do fancy regex stuff
}
If you will have a format like that iFrame you could use split and I did it using the follwoing code:
extractYoutubeLinkfromIframe(iframeTag) {
let youtubeUrl = iframeTag.split('src');
youtubeUrl = youtubeUrl[1].split('"');
return youtubeUrl[1];
}
First we split by the src, so, we will separte the iFrame string, after that, we split by quote ", to get just the part that we need as the link is with "[link]", we get the first position that will indicate that we want to get the link.
Related
I'm looking for a Regex to look for html tags based on their class name, and extract their value, for example:
<span class="myclass" id="myid">Hello world</span>
I need to extract - Hello world
I've tried doing that by my own but it seems to be more complicated than it looks
Some help? :)
Thanks!
You can try
var str = '<span class="myclass" id="myid">Hello world</span>';
var res = str.match("<([A-Za-z][A-Za-z0-9]*)\\b[^>]*>(.*?)</\\1>");
alert(res[2]);
I really prefer use a HTML parser.
But, if it is really needed, you can try this https://regex101.com/r/xP5kG7/1
.+(?<="myclass")[^>]+>([^<]+).+
It will give you the desirable output.
Lets say i have
input string as
<div id="infoLangIcon"></div>ARA, DAN, ENGLISHinGERMAN, FRA<div id="infoPipe"></div><div id="infoRating0"></div><div id="infoPipe"></div><div id="infoMonoIcon"></div>
so i want to check if inforating is 0 and then remove the div and previous div also. The output is
<div id="infoLangIcon"></div>ARA, DAN, ENGLISHinGERMAN, FRA</div><div id="infoPipe"></div><div id="infoMonoIcon"></div
Regex is not your best option here. It is not reliable when it comes to HTML.
I suggest you use DOM functions to do this (I gave you a Javascript example, you have not provided a language to be used). If I understood correctly, if there is an element with the ID of infoRating0, you want to remove it and its previous sibling. This little snippet should do that:
if (document.getElementById('infoRating0')) {
var rating0=document.getElementById('infoRating0'),
rParent=rating0.parentNode;
rParent.removeChild(rating0.previousSibling);
rParent.removeChild(rating0);
}
Also, your HTML is invalid. You can only use an ID once in your HTML. You have two divs with the same ID (infoPipe) which you should REALLY fix. Use classes instead.
jsFiddle Demo
string: "Here is the badges, https://stackoverflow.com/badges bla bla bla"
If string contatins a link (see above) I want to parse the website title of that link.
It should return : Badges - Stack Overflow.
How can i do that?
Thanks.
#!/usr/bin/perl -w
require LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
my $response = $ua->get('http://search.cpan.org/');
if ($response->is_success) {
print $response->title();
}
else {
die $response->status_line;
}
See LWP::UserAgent. Cheers :-)
I use URI::Find::Simple's list_uris method and URI::Title for this.
Depending how the link is given and how you define title, you need one or other approach.
In the exact scenario that you have presented, getting the URL with URI::Find, HTML::LinkExtractor etc, and then my $title=URI->new($link)->path() will provide the title and the link.
But if the website title is the linked text like badged, then How can I extract URL and link text from HTML in Perl? will give you the answer.
If the title is encoded in the link itself and the link is the text itself of the link, how do you define the title?
Do you want the last bit of the URI before any query? What happens with the queries set as URL paths?
Do you want the part between the host and the query?
Do you want to parse the link source and retrieve the title tag if any?
As always going from trivial first implementation to cover all corner cases is a daunting tasks ;-)
I want to display a specific message based on the URL request on a JSP.
the request URL can be:
/app/cars/{id}
OR
/app/people/{id}
On my messages.properties I've got:
events.action.cars=My car {0} event
events.action.people=My person {1} event
Finally, on my JSP page I want to have the following code:
<spring:message code="events.${element.cause}.${?????}"
arguments="${element.param['0']},${element.param['1']}"/>
I need help figuring out which expression I could use to parse the request URL and obtain the word before the ID.
You can access the request URI in JSTL (actually: EL) as follows:
${pageContext.request.requestURI}
(which thus returns HttpServletRequest#getRequestURI())
Then, to determine it, you'll have to play a bit round with JSTL functions taglib. It offers several string manipulation methods like split(), indexOf(), substringAfter(), etc. No, no one supports regex. Just parse it.
Kickoff example:
<c:set var="pathinfo" value="${fn:split(pageContext.request.requestURI, '/')}" />
<c:set var="id" value="${pathinfo[pathinfo.length - 1]}" />
And use it as ${id}.
/app/(cars|people)/([^/]*)$
will put cars or people in backreference \1, depending on the match, and whatever is left right of the last slash in backreference \2.
My solution so far is to have a RequestUtils class that match the regex ".?/jsp/(\w+)/..jsp" and return the group(1).
in my Jsp I got:
<% request.setAttribute("entity", RequestUtils.getEntityURI(request)); %>
<spring:message code="events.${element.cause}.${entity}"
arguments="${element.param['0']},${element.param['1']}"/>
this of course did the trick. But still it would be better not to have any Java code within the JSP.
If I understand you correctly, I think you need to do something like this:
#RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(#PathVariable String ownerId, Model model) {
model.addAttribute("ownerId", ownerId);
return "myview";
}
As you can see, here the ownerId is read from the URL by Spring MVC. After that, you simply put the variable in the Model map so you can use it in your JSP.
I want to extract the image url from any website. I am reading the source info through webRequest. I want a regular expression which will fetch the Image url from this content i.e the Src value in the <img> tag.
I'd recommend using an HTML parser to read the html and pull the image tags out of it, as regexes don't mesh well with data structures like xml and html.
In C#: (from this SO question)
var web = new HtmlWeb();
var doc = web.Load("http://www.stackoverflow.com");
var nodes = doc.DocumentNode.SelectNodes("//img[#src]");
foreach (var node in nodes)
{
Console.WriteLine(node.src);
}
/(?:\"|')[^\\x22*<>|\\\\]+?\.(?:jpg|bmp|gif|png)(?:\"|')/i
is a decent one I have used before. This gets any reference to an image file within an html document. I didn't strip " or ' around the match, so you will need to do that.
Try this*:
<img .*?src=["']?([^'">]+)["']?.*?>
Tested here with:
<img class="test" src="/content/img/so/logo.png" alt="logo homepage">
Gives
$1 = /content/img/so/logo.png
The $1 (you have to mouseover the match to see it) corresponds to the part of the regex between (). How you access that value will depend on what implementation of regex you are using.
*If you want to know how this works, leave a comment
EDIT
As nearly always with regexp, there are edge cases:
<img title="src=hack" src="/content/img/so/logo.png" alt="logo homepage">
This would be matched as 'hack'.