regex replace in dreamweaver - regex

I'm trying to replace tags in my code, but keeping the text inside "as is".
example string:
<p class="negrita">text1</p>
or
<p class="negrita">text2</p>
i need to get those replaced as so:
<h3>text1</h3>
and
<h3>text2</h3>
i'm searching (and matching fine) with this,
<p class="negrita">([^>]*)</p>
but I have no idea on how to keep the text inside, as
<h3>$1</h3>
is not working.

Instead of using regular expressions to do this, use Dreamweaver's Specific Tag search option. While regular expressions are possible in Dreamweaver, it's a bit crippled and sometimes a little buggy.
To do this with Specific Tag, invoke the search using Control-F
Change the search option to "Specific Tag".
Next to that, enter p to search for all <p> tags
Hit the + icon below the box to add an option and select "With Attribute" "Class" "=" from the first three pulldowns and type "negrita" in the fourth.
For action, select "Change Tag" and then select h3.
Run this and you will get <h3 class="negrita">text1</h3>
Then repeat the steps above, searching for all h3 tags with the class negrita and choose Remove Attribute "class" for the action. Two steps to one, I admit but it will work every time.

Related

bbedit - how to change multiple strings to title case?

In bbedit there is a feature where you select the text, choose text->change case->make title case from the menu and it will act accordingly.
Is there a way to select multiple strings of text across files in your project then apply the same text formatting?
I know you can do some regex stuff and change it, but none of that is the true title case where it ignores the words like "of" "and" "the" etc. The title case works great, I just need to do it on many items.
For example 5 html files have <h2>THIS IS THE TITLE</h2> -so now I go to each file select the text and do the above menu item. That is fine if there are 5, but if there are 2500 that I want to make <h2>This is the Title</h2> - then I need to be able to select more than one at a time....
Thanks in advance!
------edits
So if you were searching across multiple files for all your <h2> tags and you get back several across different files.....
<h2>MY TITLE</h2>
<h2>this is a title</h2>
<h2>Another title</h2>
The title case would change each of them accordingly to:
<h2>My Title</h2>
<h2>This is a Title</h2>
<h2>Another Title</h2>
Currently you select each one individually to do so via the menu. We'd like to do this with a find-all and change case if that makes sense....
F:<h2>(.*?)</h2>
R: <h2>\1</h2>[make this a certain case]
Thanks.
Almost any operation which involves repetition in BBEdit can be automated using AppleScript. :-)
Here's the text of an AppleScript script which will perform the operation you describe. You can copy and paste this into the AppleScript editor, and save it in BBEdit's "Scripts" folder for future use as needed.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "BBEdit"
-- make sure we start at the top, because searches will (by default) proceed from the end of the selection range.
tell text of document 1 to select insertion point before first character
repeat
tell text of document 1
-- Find matches for a string inside of heading tags (any level)
-- NOTE extra backslashes in the search pattern to keep AppleScript happy
set aSearchResult to find "(<(h\\d)>)(.+?)(</\\2>)" options {search mode:grep}
end tell
if (not found of aSearchResult) then
exit repeat -- we're done
end if
-- the opening tag is the first capture group. We'll use this below
set openingTagText to grep substitution of "\\1"
-- the title is the third capture group
set titleText to grep substitution of "\\3"
-- use "change case" to titlecase the title
set changedTitleText to change case (titleText as string) making title case
-- select the range of text containing the title, so that we can replace it
set rangeStart to (characterOffset of found object of aSearchResult) + (length of openingTagText)
set rangeEnd to (rangeStart + (length of changedTitleText) - 1)
select (characters rangeStart through rangeEnd of text of document 1)
-- replace the range
set text of selection to changedTitleText
-- select found object of aSearchResult
end repeat
-- put the insertion point back at the top, because it's a nice thing to do
tell text of document 1 to select insertion point before first character
end tell

Regular Expression, searching code in Dreamweaver

I just need to search some code and find the following, using the find and replace with regular expressions in Dreamweaver:
<div id ="specific_div"> anything can be in here </div>
I have not really tried to tackle regular expressions yet, but I am having trouble figuring out how to tell it that "I don't care what is in between the div tags, but please find me this specific div tag, everything in it and the closing div tag!"
There's a 'specific tag' option under the 'search' list in the 'Find and Replace' window. Why don't you use it? Anyway I'd probably use something like the following. But it won't select all the node if there's an idenatical closing tag <div> inside that div.
<div\b(.*?)(\bid)\s*=\s*("|')specific_div\3([\w\W]*?)</div>
I didn't close the opening tag because I thought you might have other attributes in that node. Oh and make sure the 'Match Case' option is not selected in the search window.

REGEX Pattern - How do I match upto a certain tag in html

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>/

Regular Expression matching nested TAGS

Hello I'm trying to match multi-nested quote's blockquotes and transform them back into BBCode
This is what I got so far as far as regex is involved
Converted it back to html entities to be seen on stackedoverflow
<div class="quoteheader"><div class="topslice_quote">([\s\S]*?)</div></div><blockquote>([\s\S]*?)(?:</blockquote><div class="quotefooter"><div class="botslice_quote"></div></div>){2,})
I'm trying to match this
<div class="quoteheader"><div class="topslice_quote">Quote</div></div><blockquote>Outside quote is this
<div class="quoteheader"><div class="topslice_quote">Quote</div></div><blockquote>Inner quote is this</blockquote><div class="quotefooter"><div class="botslice_quote"></div></div>
</blockquote><div class="quotefooter"><div class="botslice_quote"></div></div>
to generate this
[quote]Outside quote is
this[quote]Inner quote is
this[/quote][/quote]
I'm using VBScript 5.5 Regeular Expressions for this. (but this isn't that important)
I really need help on the expression. I've tired using a HTML Parser for this but it turns out to be more difficult then using regex
I'm just repeating what's said here.
Regular Expressions can't match Context Free languages, like groups of tags. You can't match opening to closing tags, so parsing a block (Especially a nested one) becomes impossible to do reliably.
You can certainly build a cludge to help, but there will be situations where it won't work.
Well, this is all you need to do with the parser.
Here's the pseudocode. I don't know your parser so this is the best I can offer.
First find the div tag with the quoteheader class. Get the next sibling.
That is the blockquote tag. Let's call this tag theQuote.
Get the first child of theQuote. It will be a html text item. That is the outer quote.
Get the third child of theQuote. It will be another blockquote tag. Let's call this tag theInner.
Get the first child of theInner. It will be a html text item. That is the inner quote.

Delete all lines until a specific line in Notepad++

I have to edit a lot of source codes similar to each other.
random blah
random blah
blah
<table style="width: 232px; font-size: small;" cellpadding="0" cellspacing="0">....
What I want to do is to delete lines until table tag. I think I can do it with Regex search but I couldnt write the regex pattern.
Thank you
You have to go through multiple steps to do what you stated above:
Go to the replace window, select the "extended" mode, and in the "find what" field type in "\r\n" and replace them with: "LINEBREAK" (theres a space after 'LINEBREAK'). Click on replace all.
Go to the replace window again, select the "regular expression" mode, and in the "find what" field type in "(.*)(.*)(<table)(.*)(>)(.*)(.*)" and in the replace with field, type in "\2\3\4\5". Click on replace all.
Now go to replace window again, select elect the "extended" mode, and in the "find what" field type in "LINEBREAK" (theres a space after 'LINEBREAK') and replace them with: "\r\n". Click on replace all.
Notepad++ doesn't support multi line regex, which makes it hard to do what you wanted to do without going through the steps given above.
you can try something like:
(^.*$\n)*<table(.+)>
First group will match all lines before your table tag %)