I am trying to pull a value from my HTML Source code and use it as a Grep Extract.
Using Burp Suite's 'Grep - Extract', how do I extract the following text value (in this case the text is hello, but it changes every time and I want to be able to extract the value of text=)?
<div id="CaptchaImage">
<img src="Captcha.ashx?text=hello">
</div>
Define at start and end?
Or Extract from regex group?
I can't seem to get this to work.
Define at start and end, start after expression src="Captcha.ashx?text=, end at delimiter ".
Alternatively, define from regex group src="Captcha.ashx\?text=([^"]+)". And here's hoping I didn't forget any backslashes in there.
Related
I've got this file from google that has these js attributes like jsname="data", jscontroller="data" etc.
I'd like to use Atom's find and replace with regex feature to replace all attributes beginning with js*="*" with blanks.
How would the regex for this be?
So <div class="l-o-c-qd" jsname="name" jscontroller="somecontroller">Text</div>
will be <div class="l-o-c-qd">Text</div>
Search correct RegEx corresponding to js*="*" en replace it with nothing (check space before/after for avoid double spaces after replacement)
im rubbish with regex if someone could help id be very appreciative.
its going to be a bit of a tough one i imagine - so my hats off too anyone that can solve it!
so say we have file that contains 2 html tags in the following formats:
abc1234
Some Text <P>
Some Text
abc1234
im trying to remove everything in those tags except the url (and leaving other text) so the output of the regex in this document would be
abc1234
http://google.com <P>
http://www.google.com
abc1234
Can any guru figure this one out? Id prefer one regex expression to handle both cases but two seperate ones would be fine too.
Thanks in advance/
ScottStevens, it is well known that trying to parse html with regex is difficult, in fact, there is quite a verbose post on this issue. However, if those are the only two formats the <a> ever takes, here is the approach to the problem:
Your first clue on how to approach this problem is that both tags start with <a href=", and you want to take that out, and for that, a simple remove on '<a href="' will do, no regex required.
Your next clue is that sometimes, your end tag sometimes has ">...</a> and sometimes has " rel=...</a> (what goes between rel= and doesn't matter from a regex point of view). Now notice that " rel="...</a> contains within it somewhere a ">...</a>. This means you can remove " rel="...</a> in two steps, remove " rel="... up to the ">, and then remove ">...</a>. Additionally, to make sure you remove between only one tag of <a...>...</a>, add the additional constraint that in the ... of ">...</a>, there cannot be any <a.
That and a regex cheat sheet can help you get started.
That said, you should really use an html parser. Robust and Mature HTML Parser for PHP
I'm a Rubyist, so my example is going to be in Ruby. I'd recommend using two regexes, just to keep things straight:
url_reg = /<a href="(.*?)"/ # Matches first string within <a href=""> tag
tag_reg = /(<a href=.*?a>)/ # Matches entire <a href>...</a> tag
You'll want to pull the URL with the first regex out and store it temporarily, then replace the entire contents of the tag (matched with the tag_reg) with the stored URL.
You might be able to combine it, but it doesn't seem like a good idea. You're fundamentally altering (by deleting) the original tag, and replacing it with something inside itself. Less chance of things going wrong if you separate those two steps as much as possible.
Example in Ruby
def replace_tag(input)
url_reg = /<a href="(.*?)"/ # Match URLS within an <a href> tag
tag_reg = /(<a href=.*?a>)/ # Match an entire <a href></a> tag
while (input =~ tag_reg) # While the input has matching <a href> tags
url = input.scan(url_reg).flatten[0] # Retrieve the first URL match
input = input.sub(tag_reg, url) # Replace first tag contents with URL
end
return input
end
File.open("test.html", "r") do |html_input| # Open original HTML file
File.open("output.html", "w") do |html_output| # Open an output file
while line = html_input.gets # Read each line
output = replace_tag(line) # Perform necessary substitutions
html_output.puts(output) # Write output lines to file
end
end
end
Even if you don't use Ruby, I hope the example makes sense. I tested this on your given input file, and it produces the expected output.
I have the following content
<li>Title: [...]</li>
and I'm looking for regex that will match and replace this so that I can parse it as XML. I'm just looking to use a regex find and replace inside Sublime Text 2, so I want to match everything in the above example except for the [...] which is the content.
Why not extract the content and use it to build the xml rather than trying to mold the wrapper of the content into xml? (or am i mis understanding you?)
<li>Title: ([^<]*)<\/li>
is the regular expression to extract the content.
Its pretty self explanatory other than the [^<]* which means match any number of characters that is not a "<"
I don't know Sublime, but something like this should suffice to get you the contents of the li. It allows for there being optional extra attributes on the tag. Make sure and turn off case-sensitivity, incase of LI or Li etc. (lifted straight from http://www.regular-expressions.info/examples.html ):
<li\b[^>]*>(.*?)</li>
<li>\S*(.*)?</li>
That should match your string, with the content being capturing group 1.
I have some html which I want to grab between 2 tags. However nested tags exist in the html so looking for wouldn't work as it would return on the first nested div.
Basically I want my regex to..
Match some text literally, followed by ANY character upto another literal text string. So my question is how do I get [^<]* to continue matching until it see's the next div.
such as
<div id="test"[^<]*<div id="test2"
Example html
<div id="test" class="whatever">
<div class="wrapper">
<fieldset>Test</fieldset><div class="testclass">some info</div>
</div>
<!-- end test div--></div>
</div>
<div id="test2" class="endFind">
In general, I suspect you want to look at "greedy" vs "lazy" in your regex, assuming that's supported by your platform/language.
For example, <div[^>]*>(.*?)</div> would make $1 match all the text inside a div, but would try to keep it as small as possible. Some people call *? a "lazy star".
But it seems you're looking to find the text within a div that is before the start of the first nested div. That would be something like <div[^>]*>(.*?)<div
Read about greedy vs lazy here and check to make sure that whatever language you're using supports it.
$ php -r '$text="<div>Test<div>foo</div></div>\n"; print preg_replace("/<div[^>]*>(.*?)<div.*/", "\$1", $text);'
Test
$
Regex is not capable of parsing HTML. If this is part of an application, you're doing something wrong. If you absolutely have to parse a document, use a html/xml parser.
If you're trying to screen scrape something and don't want to bother with a parser, look for identifying marks in the page you're scraping. For example, maybe the embedded div ends just before the one you want to match, so you could match </div></div> instead.
Alternatively, here's a regex that meets your requirements. However, it is very fragile: it will break if, for example, #test's children have children, or the html isn't valid, or I missed something, etc, etc ...
/<div id="test"[^<]*(<([^ >]+).+<\/$2>[^<]*)*<\/div>/
I have an apparently simple regex query for pipes - I need to truncate each item from it's (<img>) tag onwards. I thought a loop with string regex of <img[.]* replaced by blank field would have taken care of it but to no avail.
Obviously I'm missing something basic here - can someone point it out?
The item as it stands goes along something like this:
sample text title
<a rel="nofollow" target="_blank" href="http://example.com"><img border="0" src="http://example.com/image.png" alt="Yes" width="20" height="23"/></a>
<a.... (a bunch of irrelevant hyperlinks I don't need)...
Essentially I only want the title text and hyperlink that's why I'm chopping the rest off
Going one better because all I'm really doing here is making the item string more manageable by cutting it down before further manipulation - anyone know if it's possible to extract a href from a certain link in the page (in this case the 1st one) using Regex in Yahoo Pipes? I've seen the regex answer to this SO q but I'm not sure how to use it to map a url to an item attribute in a Pipes module?
You need to remove the line returns with a RegEx Pipe and replace the pattern [\r\n] with null text on the content or description field to make it a single line of text, then you can use the .* wildcard which will run to the end of the line.
http://www.yemkay.com/2008/06/30/common-problems-faced-in-yahoo-pipes/