How to consider a string with % as raw string and not as variable in Finalbuilder find and replace action.? - replace

I need to find and replace a particular string say {% java %} in all the html files and replace it with java. But in the file iterator while i use the find and replace method , it throws error as
Expanding log title failed : Variable : java - does not exist!
but in my case {% java %} is not a variable but actual string itself.
I tried "whole words only" option, which was previously selected as "Match as regular expression" under the Find tab of the action.

Related

Regex Substitution to add a class to a specific html tag

I need to do a regex find-replace on the post content of a wordpress site in order to change all existing <h4> tags to <h2> tags. I then need to style the <h4> tags to look like <h2> tags.
My plan was to add a class to the new <h2> tags...
<h4> Some poorly written html </h4>
becomes
<h2 class="pseudo-h4"> Some poorly written html </h2>
I feel like this should be doable with regex, but I just cannot seem to grok the more advanced parts of regex. My current working approach is to use this regex (?<=h4)(.+class=") to capture the 'class=' part of any h4 opening tag and then use $1pseudo-h4 as the substitution string. Once that is done I can go back and replace all h4s without regex because those which are "pseudo-h4s" will already be marked by the class.
I have a few problems...
1 - wp-cli is hanging when I try to run this on wp_posts. Maybe this is normal?
2 - $1pseudo-h4 with a space on the end is needed prevent my class from concatenating with the next class, but when i pass the argument with a space on the end i get "unknown --regex  parameter"
3 - In my tester it worked, but I dont actually know why this pattern wont match the tag of a previous element, for instance...
<h4>Sup<h4><p class="extra-cheese">Bla bla<p>
my lookbehind should see the <h4> and .+ should go through as many characters as it needs to hit the "class=" section right?

MultiLine Regex search in VisualStudio?

I need to insert into DB all text literals that are in a web page.
Looking at
<article>
<h2 class="f5_Heading">
Marketing
</h2>
</article>
I need to find "Marketing"
So I did this regex search in Visual studio search :
\>[^\<]#\<
But it doesn't find it. however it does find this :
Notice : there is no line breaks .
Question:
I don't understand why it doesn't find it in the first Regex :
The [^\<]# means : anything which is not a closing tag characters.
How can I enhance my regex to match also multilines ?
You can use the following regex
(?<=>)\n?\s*\w+
The above will match the "Marketing" and also the spaces before it. In C# you would want something like
(?m)(?<=>)\n?\s*\w+
DEMO

preg_replace replace incomplete tags

i am trying to read a html page using file_get_contents. After I processed the data, there are some incomplete tags for example:
</p><p> test test test test</p>
In this case there does not have a <p> to open </p>
or
<font color="#333333">abc</font><div><p>go go go go </p>
in this case there does not have a </div> to close<div>
thus I want to use preg_replace to remove all these incomplete tags, in my examples, the extra </p> and <div> should be removed. How can I do that? these tags can be any valid html5 tags.
First, you need to understand what a "well formed markup document" is in XHTML.
With well formed markup it does not guarantee the tags chosen as a "start end pair(open close)" will be the correct two if their is a spare unpaired tag.
Second, you will need to build a loop to call each tag per iteration from an array repository of the tag types. The tags in the array should be "literals".
Each tag "length" int should be taken and set in the loop before testing for the tag presence.
When the match of the tag pair(open close) is found, preg match puts the section onto an array of copy of matches,position and length, then take the length of the match and its start position from the parts of the preg match return result array(use a debug print-out of the array while developing the script).
Inside each open close pair matched you need to do a sub loop of the same action to check internal tags.
Synopsis:
To build such a system as a customised script ranks with an XML well formed document parser and debugger having any valid efficiency.As much it would be a markup debugger for an IDE if it had that as valid efficiency.
Good luck.
You should investigate the use of the PHP Tidy extension (http://php.net/manual/en/book.tidy.php). You can use Tidy to clean up malformed HTML based on whatever DOCTYPE you are attempting to validate.

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

regex replace in dreamweaver

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.