Sublime Text Regex Search for alphanumeric string, not working.. - regex

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>

Related

Regex to match two factors in one?

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

Parse specific div from raw text using regex?

So I'm in a situation that requires parsing raw HTML data as a string, this is unavoidable unfortunately otherwise I wouldn't post this. I only need regex to match the class of a div that has an img tag as a child.
So this is the code example that I'm dealing with:
<div class="summary">
<h3>Example</h3>
<div class="explanation">
<span>This serves as an example for the site.</span>
</div>
<div class="user-details">
mheathershaw<br>
<img src="res/badge522.png"/> <span class="score">522</span>
</div>
<div class="help">
Help
</div>
</div>
And the div that I'd like to retrieve the class from is the div that contains the image. The exact capture from this example that I'd like (optimally) is user-details. The criteria for capturing it is simply if it has <img ... /> as a child.
Anyone able to help? Thanks!
You may try this,
/<div\b[^>]*\bclass="([^"]*)"[^>]*>(?:(?!<\/div>)[\s\S])*?<img\b[^>]*>(?:(?!<\/div>)[\s\S])*?<\/div>/
DEMO

Multiline regex find third div in div group

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>

Regular expression for exactly one match

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.

Regex - Remove all other texts except matching text

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