I have some html files that I want to find them if these words:
id="s"
doesn't have this tag before them
<div
And if it doesn't, the regex adds it, so the result be:
<div id="s"
And leaves everything before the div or after id="s" as is.
Given that:
the id="s" could have spaces or tabs before it
it has some words after it
the missing "< div" could be before it with a line or in the same line before the id="s"
The simplest approach would probably be to search for all occurrences of either <div id="s" or id="s" that follows any whitespace and replace either one with <div id="s". (That is, rather than looking for id="s" then looking backwards for <div, just include <div in the search.)
Find what: <div\s*id="s"|\s*id="s"
Replace with: <div id="s"
Before:
test
test <div
id="s"
test
id="s"
test <div id="s"
test id="s"
<div id="s"
test id="s"
<div id="s"
After Replace all:
test
test <div id="s"
test<div id="s"
test <div id="s"
test<div id="s"
<div id="s"
test<div id="s"
<div id="s"
You can either add leading whitespace to the search and replace fields, or do another pass to put however much space you need back between test<div id="s".
Related
Using <div dir=.*?> works fine to match <div dir="auto">.
However, why does <div dir=.*?><br \/> not match <div dir="auto"><br />?
Code: https://regex101.com/r/5pP38n/1
The regexp starts matching at the first <div dir= in the input. Then it looks for the next ><br \/> in the input. .*? will match everything between them, which is
"auto">Please 🙏 sir my youtube channel delete <div dir="auto"
You don't match <div dir="auto"><br /> because it's contained inside this match, and a regexp doesn't return overlapping matches.
If you don't want .*? to match across multiple tags, you can use [^>]* instead.
<div dir=[^>]*><br \/>
DEMO
I'm trying to replace a common theme used in hundreds of pages in my project:
<div id="PageTitle"> (Page title as a string) </div>
And the title varies each page. I want to replace it with
<div class="row">
<div class="col-md-12 col-sm-12">
<h3><?= $pageTitle?></h3>
</div>
</div>
I've tried searching with <div id="PageTitle">/^\w+$/</div>, and <div id="PageTitle">"^[a-zA-Z0-9_]*$"</div> with no luck. Any ideas?
You are almost there. Looks like you got the pattern from somewhere else. ^ and $ are starting and ending anchors so they match with the start and end of an input so you should probably get rid of them.
Next if your page title is only going to contain alphanumeric characters (no spaces too) then \w is fine, else you might want to use . instead.
<div id="PageTitle">\w+<\/div>
For a title containing any character:
<div id="PageTitle">.+?<\/div>
Here's a demo
Hope this helps!
Try this one as well, I think its pretty strict:
<div id="PageTitle">(?:(?!<\/div>).)+<\/div>
Or even:
<div id="PageTitle">[\s\S]*?<\/div>
How can I write a regular expression to find the third div class name in a group div? So in the instance below, I'd like to find clsA3
<div class="clsA">
<div class=clsA1">blah</div>
<div class=clsA2">blah</div>
<div class=clsA3">blah</div>
</div>
I'm trying to use Visual Studio 2013 Search and Replace using Regular Expression option. The purpose of this task is one-time maintenance task.
Works with VS2010. Not sure if VS2013 does normal regex yet.
Find:
{\<div class=[^>]*\>[ \n]*(\<div class=[^>]*\>[^<]*\</div\>[ \n]*)^2\<div class="#}[^">]+{"#\>[^<]*\</div\>[ \n]*\</div\>}
Replace:
\1Hello World\2
Output:
<div class="clsA">
<div class=clsA1">blah</div>
<div class=clsA2">blah</div>
<div class=Hello World">blah</div>
</div>
I am using the following regular expression in my code editor (sublime text) in order to search for the ASP.NET comments.
<%--.*(\n.*)*--%>
I want this regular expression to stop looking any forward as soon as the first --%> is found. But it keeps looking until the last comment's --%> is found. I have got this idea that i've to use some kind of flag to make it stop as soon as the first --%> but I am unable to figure it out.
Can anyone please tell me how may I modify this regex?
UPDATE
I forgot to post some sample markup. Here it is:
<div class="modal-footer">
<%--<button class="btn" data-dismiss="modal">
Close</button>
<button id="btnAddCountry" class="btn btn-primary" data-dismiss="modal">
Save changes</button>--%>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<div class="box paint_hover">
<div class="title">
<h3>Sale Voucher</span>
</h3>
</div>
<div class="content">
<ul id="tabExample1" class="nav nav-tabs">
<li class="active"><a id="lnkAddEditVoucher" href="#AddEditVoucher" data-toggle="tab">Add/Update Sale Voucher</a></li>
<li><a id="lnkViewVouchers" href="#ViewVouchers" data-toggle="tab">Search Sale Voucher</a></li>
<%-- <li><a id="lnkViewParties" href="#ViewParties" data-toggle="tab">Search Parties</a></li>--%>
</ul>
I just want to match the first comment and not the second one.
You need to make the * quantifiers non-greedy. Usually this is done by adding a ? after them, e.g. .*? instead of just .*.
I've also simplified the regex a bit. Sublime Text supports the (?s) modifier at the beginning of the pattern to make the dot match even newlines:
(?s)<%--.*?--%>
If you prefer matching the newline explicitly:
<%--(.|\n)*?--%>
The problem you seem to have is that you use the greedy version of .*, which matches anything (including --%>). Try using <%--.*?(\n.*?)*?--%> instead to make it non-greedy.
I'm using sublime text 2 editor. I would like to remove all other texts except the name tag value.
Here is an example
<div class="control-group">
<label class="control-label" for="c_email">Email (required)</label>
<div class="controls">
<input type="text" name="c_email" value="">
</div>
</div>
In the above code I would like to keep only the name tag value. To match all name tags I use regex like this
name="(.+?)"
It matches all name tags. But when I replace it with $1 it only replaces name="c_email" with c_email
Can someone help me to erase all other texts?
Thanks
you can do it like that:
find: [\s\S]*?name="(.+?)"[\s\S]*
replace: $1
if you want to limit deletion to your target div:
find: <div class="control-group"[\s\S]*?name="(.+?)"[\s\S]*?</div>\s*</div>
replace: $1