regex using Sublime - regex

Using my text editor of choice, Sublime 2, I want to search through code that has uncommented alerts. So I need a regex for that finds "alert" but not "//alert" or "// alert". I don't know how to invert and then combine the two results. Sublime Text uses the Boost syntax for regular expressions. Thank you for any help.

You can search for text not preceeded by //, thus
(?<!\/\/\s?)alert
EDIT:
If the editor doesn't support variable lookbehinds you must specify all the possibilities in different lookbehinds
(?<!\/\/\s)(?<!\/\/)alert

try this:
(?<!//)(?<!// )alert
Boost syntax is based on Pearl RegExp. Thus negative lookbehind (?<!text) should be supported. In this example I use the negative lookbehind twice (with and without space) because the lookbehind text has to be fixed length.
you can read more about lookaraound feature in RegExp here:
http://www.regular-expressions.info/lookaround.html

Related

Negative lookbehind does not work with multiple alternatives in FileLocatorPro regex

(?<!ing|how|out)\sto\b
the expression I used in FileLocatorPro is ok, but after I added some words in it, like
(?<!ing|how|out|wants)\sto\b
it went wrong. Is there any limit of using "|"?
The Regular Expression flavor used for the Perl compatible option is Boost, see the FileLocatorPro docs:
Perl compatible regexp syntax is based around the Boost regular expression engine and includes not only the functionality of the 'classic' regular expression engine but also additional Perl style expression enhancements detailed here: http://www.boost.org/doc/libs/release/libs/regex.
Boost docs say that (?<!pattern) consumes zero characters, only if pattern could not be matched against the characters preceding the current position (pattern must be of fixed length).
That means, all alternatives inside a lookbehind must be of the same length.
The work around is to chain the lookbehinds with alternatives of the same length:
(?<!ing|how|out)(?<!wants)\sto\b
See the regex demo (Python option is used because Python has the same lookbehind length restriction).

Complex regular expression ... AND OR, negation

I would like to search files by their content in Total Commander so I want to create a regex, but I cannot find any manual where it would really be explained. My situation is that I need something like this:
fileContains("<html>") && fileContains("{myVariable1}") && fileNotContains("<script>")
I can write cca this:
(<html>)+
({myVariable1})+
(<script>){0} ... but this does not work for me
And I cannot put it all together. Any ideas, please? Or do you have a link to an excellent regex explanation?
try this regex:
(?=.*\{myVariable1\})(?=.*<html>)(?!.*<script>)
it's just 3 lookaheads in a row. one of those is a negative lookahead. Note the "single line" modifier to enable 'dot matches newline'.
edit (per comment): I guess Total Commander's regex engine does not support lookarounds at all. While you could combine two positive lookaheads into an equivalent 'consuming' pattern with something like this untested regex: (.*(\{myVariable1\}|<html>)){2}, you cannot include the 'negated search' within a single regex unless you have a legitimate regex engine.
You could try this Total Commander regex plug-in:
A RegEx content plug-in with Unicode support - based on Perl
Compatible Regular Expressions (PCRE) library. This plug-in may
replace TC's RegEx engine for file content

Removing Japanese text between two vertical bars using regex and notepad++

I have a text like this
English text||Arabic text||Japanese text||Arabic text||numbers
I tried using (\|\|\p{Han}\p{Hiragana}\p{Katakana}\|\|) but I'm getting "invalid Regular Expression" error message in notepad++, although it's right as I tested it in This regex tester, Plus this will only look at the Japanese text with Katakana after Hiragana after Kanji, how can I make it look an the Japanese text without that order?
Notepad++ does not support \p modifier, try \p{Letter} (should match any letter in any language) and you will see no match.
You can use some other application, e.g. a very good one is EditPad.
I figured what's the main problem, I have to [\p{Han}\p{Hiragana}\p{Katakana}] if I don't want it in a specific order, so all what I had to do is find: \|\|([\p{Han}\p{Hiragana}\p{Katakana}]*?)\|\| and replace it with ||.
Of course Notepad++ didn't work, So I used EditPad as NikitOn suggested

Why would a regex work in Sublime and not in vim?

Tried searching for regex found in this answer:
(,)(?=(?:[^']|'[^']*')*$)
I tried doing a search in Sublime and it worked out (around 700 results). When trying to replace the results it runs out of memory. Tried /(,)(?=(?:[^']|'[^']*')*$) in vim for searching first but it does not find any instances of the pattern. Also tried escaping all the ( and ) with \ in the regex.
Vim uses its own regular expression engine and syntax (which predates PCRE, by the way) so porting a regex from perl or some other editor will most likely need some work.
The many differences are too numerous to list in detail here but :help pattern and :help perl-patterns will help.
Anyway, this quick and dirty rewrite of your regular expression seems to work on the sample given in the linked question:
/\v(,)(\#=([^']|'[^']*')*$)
See :help \#= and :help \v.
One possible explanation is that the regular expression engine used in Sublime is different than the engine used in vim.
Not all regex engines are created equal; they don't all support the same features. (For example, a "negative lookahead" feature can be very powerful, but not all engines support it. And the syntax for some features differs betwen engines.)
A brief comparison of regular expression engines is available here:
http://en.wikipedia.org/wiki/Comparison_of_regular_expression_engines
Unfortunately Vim uses a different engine, and "normal" regular expressions won't work.
The regex you've mentioned isn't perfect: it doesn't skip escaped quotes, but, as I understand, it's good enough for you. Try this one, and if it doesn't match something, please send me that piece.
\v^([^']|'[^']*')*\zs,
A little explanation:
\v enables very magic search to avoid complex escaping rules
([^']|'[^']*') matches all symbols but quote and a pair of qoutes
\zs indicates the beginning of selection; you can think of it as of a replacement for lookbehind.
You have to escape the |, otherwise it doesn't work under vim. You should also escape the round brackets, unless you are searching for the '(' or ')' characters.
More information on regex usage in vim can be found on vimregex.com.

Notepad++ regex to find 3 consecutive numbers

I'm trying to use Notepadd++ to find all occurrences of width=xxx so I can change them to width="xxx"
as far as I have got is width=[^\n] which only selects width=x
If you need exactly 3 numbers, the following is tested in Notepad++:
width=\d\d\d[^\d]
Reading further into your requirement, you can use the tagging feature:
Find what: width=(\d\d\d)([^\d])
Replace with: width="\1"\2
Here, the (n) bracketed portions of the regex are stored (in sequence) as \1,\2,...\n which can be referred to in the replacement field.
As a regex engine, Notepad++ is poor. Here is a description of what's supported. Pretty basic.
Looking at the Notepad++ Regular Expression list there does not seem to support the {n} notation to match n characters, so \d{3} did not work.
However, what had worked for me and may be considered a hack was: \d\d\d
Tested in Notepad++ and has worked, for the Find field use (\d\d\d) and for the Replace filed use "\1"\2.
As Tao commented, as of version 6, Notepad++ supports PCRE.
So now You can write:
\d{1,5}
/(width=)(\d+?)/gim
Because you may want variable digits. Some widths may be 8, or 15, or 200, or whatever.
If you want to specify a range, you do it like this:
/(width=)(\d{1,3)/gim
where the 1 represents the lower limit and the 3 represents the upper.
I grouped both parts of the expression, so when you replace you can keep the first part and not blow it away.
Tried it: replace width=([0-9][0-9][0-9]) with width="\1" and worked fine... Of course might not be best syntax to do this but it works...
I would try this one: width=(\d{3,}), and check Regular expression, and also . matches newline
works for me on ver: 7.5.4