Pure Regular Expression replace n by n+1 - regex

How do I replace every number n that matches a certain pattern with n+1 in VsCode?
E.g.
1,2,3,4,5,6,
Result.
2,3,4,5,6,7,

TL;DR: Regex doesn't look like the appropriate tool.
However, some regular expressions engine allow it, such as vim's one:
s/<\zs\d\+\ze>/\=(submatch(0)+1)/g
And in sublime text there is a plugin for that: https://stackoverflow.com/a/12940856/6320039
This said you could either use vim only for this task or help the community by creating a new plugin similar to the above example!

Related

How to match and replace n number of times with RegEx

I'm using TextWrangler, the free version of BBEdit on the Mac, which I understand uses the PCRE engine.
What I want to do is match a specific number of lines and replace as well.
After a lot of searching I came up with this:
(^(.*\r)){25}
This lets me match up to 25 lines. It works great, but the problem comes when I want to actually replace something. I can't figure out how to do it.
For example, I would like to replace all of the returns "\r" with tabs "\t".
Hopefully this is actually possible. I'd appreciate any help. Thanks!
Regexp domain is searching. You cannot replace using regexp; a programming language or editor can use regexp as the search part of its search-and-replace function. Thus, the way to do 25 replacements is purely in the domain of said programming language or editor. If it does not provide such capability, either directly in search-and-replace or as a macro/loop/other, then you cannot do it automatically.

Regular expression to extract argument in a LaTeX command

I have a large LaTeX document where I have defined a macro like this:
\newcommand{\abs}[1]{\left|#1\right|}
I want to get rid of it by replacing in all the document \abs{...} by \left|...\right|, so I am thinking in a regular expression. I am familiar with their basics but I do not know how to find the bracket that closes the expression, given that the following situations are possible:
\abs{(2+x)^3}
\abs{\frac{2}{3}}
\abs{\frac{2}{\sin(2\abs{x})}}
What I have been able to do for the moment is \\abs\{([^\}]*)\} and then replace as \left\1\right|but it is only able to deal with the pieces of code of the first kind only.
By the way, I am using the TeXstudio regular expression engine.
Well, I did a little more of research and I managed to solve it. According to this response in a similar question, it suffices to use recursive regular expressions and a text editor that supports them, for example Sublime Text 2 (I could not do it with TeXstudio). This does the trick:
Find: \\abs\{(([^\{\}]|(?R))*)\}
Replace: \\left|\1\\right|
EDIT 1: Actually this solves only the two first cases, but fails with the third, so any idea on how to improve the regular expression would be appreciated.
EDIT 2: See comment from #CasimiretHippolyte for full answer using \\abs\{((?>[^{}]+|\{(?1)\})*)\}

Adding up several Search and Replace regular expressions

I am new to regular expression. I want to know is there any way to batch up many 'find and replace' regular expressions together and is there any specific tool which could make this task easy?
In details-
I mean- Find one regular expression and replace with other regular expression, then find another regular expression and replace it with another different regular expression, then find third and replace it with some another, so on .. so on .. may be upto 20 search and replace. And in automated way as compared to manually doing search and replace singly upto 20 times.
Chaining Replacements
You can chain replacements in any language that gives you access to a regex engine.
Python and PHP are good choices if you are starting out and want to do a bit of scripting
Any of the .NET languages, Java, Ruby, Perl... You name it.
In Java
In the comments, you mention that you use Java. To chain replacements, you can do things like this:
String result1 = subjectString.replaceAll(myregex, myreplacement);
String result2 = result1.replaceAll(myregex2, myreplacement2);
String result3 = result2.replaceAll(myregex3, myreplacement3);
GUI Tools
I can think of three GUI tools that allow regex chaining:
PowerGrep (commercial, by Jan Goyvaerts, the author of the famous RegexBuddy)
TextDistil (free at the moment, .NET regex flavor)
TextPipe Pro (commercial)
In addition, regex chaining is available in applications with a narrow focus, for instance:
Directory Opus (powerful File Manager for Windows)
A Better Finder Rename and Name Mangler (file renamers for OSX)
In PHP you can do this with preg_replace(). If the pattern and replacement arguments are both arrays, each regexp in the pattern argument will be replaced with the corresponding element of the replacement argument.

Repeats the previous item between n and m times in wingrep

I tried the following regular expression in wingrep 2.3:
[A-Za-z0-9]{1,5}cd77
But the search returns empty, even when I do have the following string testcd77 inside txt file that I am searching.
The Regular Expression Lookup tab on the Search Criteria dialog for my copy of Windows Grep doesn't offer {} as a supported feature. So I think you're stuck with unrolling the search string to
[A-Za-z0-9]([A-Za-z0-9]([A-Za-z0-9]([A-Za-z0-9]([A-Za-z0-9])?)?)?)?cd77
Nothing in the wingrep documentation indicates that they support this kind of interval expression. You might be simply unable to do it.
Some regex engines that do support those expressions require the curly braces to be escaped, so you can try this: [A-Za-z0-9]\{1,5\}cd77. But if that doesn't work then I suspect you are out of luck.

Do calculation on captured number in regex before using it in replacement

Using a regex, I am able to find a bunch of numbers that I want to replace. However, I want to replace the number with another number that is calculated using the original - captured - number.
Is that possible in notepad++ using a kind of expression in the replacement-part?
Edit: Maybe a strange thought, but could the calculation be done in the search part, generating a second captured number that would effectively be the result?
Even if it is possible, it will almost certainly be "messy" - why not do the replacements with a simple script instead? For example..
#!/usr/bin/env ruby
f = File.new("f1.txt", File::RDWR)
contents = f.read()
contents.gsub!(/\d+/){|m|
m.to_i + 1 # convert the current match to an integer, and add one
}
f.truncate(0) # empty the existing file
f.seek(0) # seek to the start of the file, before writing again
f.write(contents) # write modified file
f.close()
..and the output:
$ cat f1.txt
This was one: 1
This two two: 2
$ ruby replacer.rb
$ cat f1.txt
This was one: 2
This two two: 3
In reply to jeroen's comment,
I was actually interested if the possibility existed in the regular expression itself as they are so widespread
A regular expression is really just a simple pattern matching syntax. To do anything more advanced than search/replace with the matches would be up to the text-editors, but the usefulness of this is very limited, and can be achieved via scripting most editors allow (Notepad++ has a plugin system, although I've no idea how easy it is to use).
Basically, if regex/search-and-replace will not achieve what you want, I would say either use your editors scripting ability or use an external script.
Is that possible in notepad++ using a kind of expression in the replacement-part?
Interpolated evaluation of regular-expression matches is a relatively advanced feature that I probably would not expect to find in a general-purpose text editing application. I played around with Notepad++ a bit but was unable to get this to work, nor could I find anything in the documentation that suggests this is possible.
Hmmm... I'd have to recommend AWK to do this.
http://en.wikipedia.org/wiki/AWK
notepad++ has limited regular expressions built in. There are extensions that add a bit more to the regular expression find and replace, but I've found those hard to use. I would recommend writing a little external program to do it for you. Either Ruby, Perl or Python would be great for it. If you know those languages. I use Ruby and have had lots of success with it.