Regex to remove attribute from xhtml document? [duplicate] - regex

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
RegEx match open tags except XHTML self-contained tags
How to remove single attribute with quotes via RegEx
I am trying to remove the "sfref" attribute from the html code below:
<a sfref="[Libraries]719c25f9-89b3-4a7c-b6d5-e734b0c06ac1" href="../../HPLC.sflb.ashx">Determination</a> <br />
<img sfref="[Libraries]3e60aebb-acac-4806-bd22-f7986f66e7b3" src="../../Note52011.sflb.ashx">Test</a><br />
So far I have come up with this regex, but it is not matching:
(sfref=")([a-zA-Z0-9:;.\s()-\,]*)(")
This is where I am testing if it help:
http://regexr.com?2v4h6
Can someone please help me remove the "sfref" attribute?

You really really really shouldn't use regex (see the link in #Jack Maney's comment), but if you have to, this should work:
sfref="[^"]*"

This will work for single or double quotes.
sfref=('|").*?\1

Related

Get link text before specific div using regex [duplicate]

This question already has answers here:
Python/BeautifulSoup - how to remove all tags from an element?
(7 answers)
Closed 3 years ago.
I'm doing some code in other to scrape a page for a specific search result, but the main problem is using regex with python.
Here is part of the website source:
<div class="title_block">
<div class="ttl-oss"> </div>
TEXT-TO-CATCH
</div>
The div ttl-oss appears just one time in the page, so my ideia is to use regex in other to search for the unique div, and get the first link text after it like (TEXT-TO-CATCH).
The problem is if I use some regex like <div class="title_block">.*?(<a.*?>)+ I'm not able to find the div and get the text.
Any new approach in how to solve it, is welcome.
Thank you
HTML is usually better handled by an HTML parser, and several are available for python. Regex in general isn't flexible enough for complicated HTML.
However, this should get the text you're looking for, assuming your page looks similar to the one you've posted as an example.
<div class="ttl-oss">[\s\S]*?<a[^>]*href.*>(.*)<\/a>
This regex looks for a div structured as you described in your example, looks for the first anchor tag it finds past that which has "href" in it, and then captures the first chunk of text after the closing >, capturing up to the closing </a> tag.
Demo

Python regex: text between <a> tag [duplicate]

This question already has answers here:
Why it's not possible to use regex to parse HTML/XML: a formal explanation in layman's terms
(10 answers)
Closed 3 years ago.
I know I got BeautifulSoup, But I want to try my own.
Regex I've been working on
<br>This Text needed
<a>unwanted text</a>
<br/>
This text needed
<a >unwanted text</a>
This text needed
<a>unwanted text</a>
<br>this text needed
What I have come up with:
(</a>|(<br(/>|>)))(\s.*|\w.*)
I want to match the This text needed but one of them isn't matching.
How about this way with lookahead negative and lookbehind positive,
(?<!<a>)This Text needed(?!<\/a>)
DEMO: https://regex101.com/r/RT5LZu/1

RegEx to match all text between two strings that slightly alter [duplicate]

This question already has answers here:
RegEx match open tags except XHTML self-contained tags
(35 answers)
Closed 3 years ago.
I am currently working on an AIR app and I'm trying to get a certain block of text from a website where that block of text is always between two specific strings that contain links that change from page to page.
It looks something like this:
<p>Previous Chapter <span style="float: right">Next Chapter</span></p>
.
.
_desired content_
.
.
<p>Previous Chapter <span style="float: right">Next Chapter</span></p>
*The two strings are identical
Now, I have tried several RegEx expressions but without success. I just can't get my head around Regex in general...
The last expression I've tried is: /(?<=<p><a href=\".+\">Previous Chapter<\/a> <span style=\"float: right\"><a href=\".+\">Next Chapter<\/a><\/span><\/p>)(.*)(?=<p><a href=\".+\">Previous Chapter<\/a> <span style=\"float: right\"><a href=\".+\">Next Chapter<\/a><\/span><\/p>)/gsi
but that one isn't even being recognized as a RegEx.
I would really appreciate any help with the subject.
Thanks in advance!
EDIT:
Thanks to Organis's help I managed to solve the problem, it was indeed easier and better NOT using RegEx.
This is what i ended up doing:
text=text.split("Next Chapter<\/span><\/a><\/p>")[1].split("Previous Chapter<\/a>")[0];
text=text.substring(0,text.lastIndexOf("<p><a href"));
Do not use RegEx. Read why: https://blog.codinghorror.com/parsing-html-the-cthulhu-way/.
Extract text between two fixed <span style="float: right">Next Chapter</span></a></p>, then cut finalizing <p>Previous Chapter <a href="**changes**"> off.

Regex to not match inside html anchor tag [duplicate]

This question already has answers here:
What is the best way to parse html in C#? [closed]
(15 answers)
Closed 5 years ago.
I have a requirement where I don't have to match a specific word when in occurs between anchor tag. Anchor tags can have other html tags nested.
For Example:
<a title="Test" href="http://www.google.com/"><span style="color: blue;">Test</span></a><p>Test - MANUALLY<br /><br />Google </p><p> Resolving as duplicate of Test</p><p>Test test</p>
Here every "Test" gets selected. All I want here is getting only "Test" not present inside "anchor tag" and also not part of attributes of "anchor tag".
Regex I used was:
(?!<a[^>]*>)(Test)(?![^<]*<\/a>)/gi
Not sure if this will accomplish your needs, but the second capturing group should only include matches that do not fall within the anchor tag.
(<a.*?<\/a>)|(test)/gi
https://regex101.com/r/rTLifk/1
However, I would highly recommend utilizing an XML parser or XPath.

Using Regular Expression find everything except two strings [duplicate]

This question already has answers here:
RegEx match open tags except XHTML self-contained tags
(35 answers)
Closed 9 years ago.
In my document I have
<Country>US</Country>
<Country>PR</Country>
Between the
<country>
and
</country>
I want to find ANYTHING except for US and PR.
For example
<country>US</country> = ignore
<country>PR</country> = ignore
<country>UP</county> = match found
What I have is
Pattern = "<Country>(.*?[^USPR].*?)</Country>"
but this ignores strings like
<Country>UP</Country>
Not sure how to write allowing only 2 options between the tags.. US and PR only.
This should work.
<country>(?!(US|PR))(.*?)</country>
Matches the opening <country> tag not followed by US or PR. Then goes on to match anything before the closing </country> tag.
Try this one:
(?<=<Country>(?!US|PR)).*?(?=</Country>)