Textmate Find regex, Replace wild - regex

In textmate-1.5 I can use the regex syntax (.*) to find both lines in the below use case:
<span class="class1"></span>
<span class="class2"></span>
Now I want to append more code to each of them so my find query is span class="(.*)" and my replace query is span class="(.*)" aria-hidden="true" which i had hoped would result in this:
<span class="class1" aria-hidden="true"></span>
<span class="class2" aria-hidden="true"></span>
but it actually resulted in this:
<span class="(.*)" aria-hidden="true"></span>
<span class="(.*)" aria-hidden="true"></span>
Using find/replace (not using column selection which would work for this example but not for the actual situation) is it possible to maintain the area matched by regex in the replace action with a representative wild character or something?

Change your replace query as,
span class="$1" aria-hidden="true"
$1 would refer the characters which are present inside group index 1.

(<span class="[^"]*")
Try this.Replace with $1 aria-hidden="true".See demo.
http://regex101.com/r/wQ1oW3/22

Related

Regex to match a string if not followed by another string

In Mediawiki via Replace extension (MariaDB 10.6) I want to match the string <span class="sense"><span class="bld">A</span> and delete it, as long as there is no <span class="bld"> further down that line. Here is an example of text where it should not be matched:
<span class="sense"><span class="bld">A</span> [[lay bare at the side]], [[expose]], τι τῆς πλευρᾶς <span class="bibl">Arr. <span class="title">Tact.</span>40.5</span>, cf. <span class="bibl">D.C.49.6</span> (Pass.). </span><span class="sense"><span class="bld">2</span> metaph., [[lay bare]], [[disclose]], τὸν πάντα λόγον <span class="bibl">Hdt.1.126</span>, cf. <span class="bibl">8.19</span>, <span class="bibl">9.44</span>; τὸ βούλευμα <span class="bibl">Conon 50</span>:—Pass., <b class="b3">παρεγυμνώθη διότι</b>… <span class="bibl">Plb.1.80.9</span>.</span>
So far I tried (<span class="sense"><span class="bld">A<\/span>) ((?!<span class="bld">).*) (and replacing with nothing) but it matches instances that do contain the unwanted string.
You can use
<span class="sense"><span class="bld">A<\/span>(?s)(?!.*<span class="bld">)
See the regex demo. Details:
<span class="sense"><span class="bld">A<\/span> - a literal <span class="sense"><span class="bld">A</span> string
(?s) - s flag that makes . match across lines
(?!.*<span class="bld">) - a negative lookahead that fails the match if, immediately to the right of the current location, there are
.* - any zero or more chars as many as possible
<span class="bld"> - a literal string.

RegEx match string but not if string comes after

I'm doing a find/replace and but I have already made a few changes the slow way. I want to use regex to replace the rest but make sure I don't replace ones I've already done. So, I need it to match 1 but not 2. The end result will be replacing all instances that look like 1 with 2. The -icon can be anything
1: <span class="glyphicons icon">
2: <span class="glyphicons glyphicons-icon">
More examples:
<span class="glyphicons hand">
<span class="glyphicons flower">
<span class="glyphicons bucket">
<span class="glyphicons glyphicons-stone_head">
<span class="glyphicons glyphicons-decapitated-corpse">
I need to replace the first 3 examples but not the last 2. The application is quite large so I'd really like to be able to do this with one 'replace all'.
Assuming icon can be any word, I'd try replacing glyphicons\s([A-Za-z]+)" by glyphicons glyphicons-$1".

Help with a regular expression

I am fairly new to regular expressions and have been having difficulty using one to extract the data I am after. Specifically, I am looking to extract the date touched and the the counter from the following:
<span style="color:blue;"><query></span>
<span style="color:blue;"><pages></span>
<span style="color:blue;"><page pageid="3420" ns="0" title="Test" touched="2011-07-08T11:00:58Z" lastrevid="17889" counter="9" length="6269" /></span>
<span style="color:blue;"></pages></span>
<span style="color:blue;"></query></span>
<span style="color:blue;"></api></span>
I am currently using vs2010. My current expression is:
std::tr1::regex rx("(?:.*touch.*;)?([0-9-]+?)(?:T.*count.*;)([0-9]+)(&.*)?");
std::tr1::regex_search(buffer, match, rx);
match[1] contains the following:
2011-07-08T11:00:58Z" lastrevid="17889" counter="9" length="6269" /></span>
<span style="color:blue;"></pages></span>
<span style="color:blue;"></query></span>
<span style="color:blue;"></api></span>
match[2] contains the following:
6269" /></span>
<span style="color:blue;"></pages></span>
<span style="color:blue;"></query></span>
<span style="color:blue;"></api></span>
I am looking for just "2011-07-08" in match[1] and just "9" in match[2]. The date format will never alter, but the counter will almost certainly be much larger.
Any help would be highly appreciated.
That's because cmatch::operator[](int i) returns a sub_match, whose sub_match::operator basic_string() (used in the context of cout) returns a string starting at the beginning of the match and ending at the end of the source string.
Use sub_match::str(), i.e. match[1].str() and match[2].str().
Moreover, you'll need your expression to be more specific: .* tries to match the world, and gives up some if it can't.
Try std::tr1::regex rx("touched="([0-9-]+).+counter="([0-9]+)");.
You could even use non-greedy matchers (like +? and *?) to prevent excessive matching.
Try
std::tr1::regex rx("(?:.*touch.*;)?([0-9-]+)(?:T.*count.*;)([0-9]+)(&.*)?");
removing the question mark makes the term greedy, so it will fill as much as it can.

Reg exp: string NOT in pattern

I have problems constructing a reg exp. I think I should use lookahead/behind but I just don't make it.
I want to make a reg-exp that catches all HTML tags that do NOT contain a string ('rabbit').
For example, the following tags should be matched
<a XXX> <span yyy> </div x zz> </li qwerty=ab cd> <div hello=stackoverflow>
But not the following
<a XXrabbitX> <span yyyrabbit> </div xrabbitzz> </li rabbit=abcd hippo=9876> <div hello=rabbit>
(My next step is to make make a substitution so that the word rabbit enters the tags, but that will hopefully come easy.)
(I use PHP5-preg_replace.)
Thanks.
I guess you're matching the HTML tags with a regex something like this:
/<[^>]*>/
You can add a negative look-ahead assertion in there to assert that "rabbit" cannot be found in the tag:
/<(?![^>]*rabbit)[^>]*>/

Regular expression to parse html links

I have this html with this type of snippit below all over:
<li><label for="summary">Summary:</label></li>
<li class="in">
<textarea class="ta" id="summary" name="summary" rows="4" cols="10" tabindex="4">
${fieldValue(bean: book, field: 'summary')}</textarea>
<a href="#" class="tt">
<img src="<g:createLinkTo dir='images/buttons/' file='icon.gif'/>" alt="Help icon for the summary field">
<span class="tooltip">
<span class="top"></span>
<span class="middle">Help text for summary</span>
<span class="bottom"></span>
</span>
</a>
</li>
I want to pull off the alt value and the text between XXXX and replace the a tag with the code below.
This is my stab at the reg ex
<a href="#" class="tt">.*alt="(.*)".*<span class="middle">(.*)<\/span><\/a>
Output with the callbacks
<ebs:cssToolTip alt="$1" text="$2"/>
I tried it out on http://rubular.com/ and it does not quite work. Any suggestions
You may want to ensure your regexp isn't greedily picking up characters - use ".*?" rather than straight ".*".
What do you mean, "it does not quite work"? How does it fail?
A suggestion (not tested your regexp): note that * is a greedy operator, so .* is rarely a good idea because it may match a lot more than what you intended.
Try:
<a href="#" class="tt">.*alt="([^"]*)".*<span class="middle">([^"]*)<\/span><\/a>
Think i solved it by getting an idea from another stackoverflow question
<a href="#" class="tt">.*alt="([^"]*)".*<span class="middle">([^<]*).*<\/a>
This seems to work on the http://rubular.com/ site
Here you go:
http://rubular.com/regexes/8434
You were facing two potential problems. First, without adding the //m option, '.' will not match newline characters. Second, you were using greedy matching. Adding the '*?' makes it better.
/<a href="#" class="tt">.*?alt="([^"]*)">.*?<span class="middle">(.*?)<\/span>/m