Adding up several Search and Replace regular expressions - regex

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.

Related

Pure Regular Expression replace n by n+1

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!

REGEX in MS Word 2016: Exclude a simple String from Search

So I read a lot about Negation in Regex but can't solve my problem in MS Word 2016.
How do I exclude a String, Word, Number(s) from being found?
Example:
<[A-Z]{2}[A-Z0-9]{9;11}> to search a String like XY123BBT22223
But how to exclude for example a specefic one like SEDWS12WW04?
Well it depends on what you need to achieve or is this a matter of curiosity... RegEx is not the same as the built-in Advanced Find with Wildcards; for that you need VBA.
Depending on your need, without using VBA, you could make use of space and return characters - something like this will work for the strings provided: [ ^13][A-Z]{2}[0-9]{1,}[A-Z]{1,}[0-9]{1,}[ ^13] (assuming you use normal carriage returns and spaces in your document)
Anyway, this is a good article on wildcard searches in MS Word: https://wordmvp.com/FAQs/General/UsingWildcards.htm
EDIT:
In light of your further comments you will probably want to look at section 8 of the linked article which explains grouping. For my proposed search you can use this to your advantage by creating 3 groups in your 'find' and only modifying the middle group, if indeed you do intend to modify. Using groups the search would look something like:
([ ^13])([A-Z]{2}[0-9]{1,}[A-Z]{1,}[0-9]{1,})([ ^13])
and the replace might look like this:
\1 SOMETHING \3
Note also: compared to a RegEx solution my suggestion is kinda lame, mainly because compared to RegEx, MS-Words find and replace (good as it is, and really it is) is kinda lame... it's hacky but it might work for you (although you might need to do a few searches).
BUT... if it really is REGEX that you want, well you can get access to this via VBA: How to Use/Enable (RegExp object) Regular Expression using VBA (MACRO) in word
And... then you will be able to use proper RegEx for find and replace, well almost - I'm under the impression that the VBA RegEx still has some quirks...
As already noted by others, this is not possible in Microsoft Word's flavor of regular expressions.
Instead, you should use standard regular expressions. It is actually possible to use standard regular expressions in MS Word if you use a special tool that integrates into Microsoft Word called Multiple Find & Replace (see http://www.translatortools.net/products/transtoolsplus/word-multiplefindreplace). This tool opens as a pane to the right of the document window and works just like the Advanced Find & Replace dialog. However, in addition to Word's existing search functionality, it can use the standard regular expressions syntax to search and replace any text within a Word document.
In your particular case, I would use this:
\b[A-Z]{2}[A-Z0-9]{9,11}\b(?<!\bSEDWS12WW04)
To explain, this searches for a word boundary + ID + word boundary, and then it looks back to make sure that the preceding string does not match [word boundary + excluded ID]. In a similar vein, you can do something like
(?<!\bSEDWS12WW04|\bSEDWS12WW05|\bSEDWS12WW05)
to exlude several IDs.
Multiple Find & Replace is quite powerful: you can add any number of expressions (either using regular expressions or using Word's standard search syntax) to a list and then search the document for all of them, replace everything, display all matches in a list and replace only specific matches, and a few more things.
I created this tool for translators and editors, but it is great for any advanced search/replace operations in Word, and I am sure you will find it very useful.
Best regards, Stanislav

Regex replace from | [duplicate]

How can I replace several different words all at once in Notepad++?
For example;
I have "good", "great" and "fine" and I want to replace them with "bad", "worse" and "not", respectively, all at once.
I know that I can replace them one by one, but the problem I am facing requires that I replace a lot of words, which is not convenient to do.
Try a regular expression replace of (good)|(great)|(fine) with (?1bad)(?2worse)(?3not).
The search looks for either of three alternatives separated by the |. Each alternative has ist own capture brackets. The replace uses the conditional form ?Ntrue-expression:false-expression where N is decimal digit, the clause checks whether capture expression N matches.
Tested in Notepad++ 6.3
Update:
You can find good documentation, about the new PRCE Regular
Expressions, used by N++, since the 6.0 version, at the TWO addresses
below :
http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html
http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/format/boost_format_syntax.html
The FIRST one concerns the syntax of regular expressions in SEARCH
The SECOND one concerns the syntax of regular expressions in
REPLACEMENT
And, if you can understand "written French", I made a tutorial about
PCRE regular expressions, stored in the personal site of Christian
Cuvier (cchris), at the address below :
http://oedoc.free.fr/Regex/TutorielRegex.zip
(Extracted from a posting by THEVENOT Guy at http://sourceforge.net/p/notepad-plus/discussion/331754/thread/ca059a0a/ )
Install Python Script plugin from Plugin Manager.
Create a file with your substitutions (e.g., C:/Temp/Substitutions.txt), separate values with space:
good bad
great worse
fine not
Create a new script:
with open('C:/Temp/Substitutions.txt') as f:
for l in f:
s = l.split()
editor.replace(s[0], s[1])
Run the new script against the text you want to substitute.
I needed to run the substitution on several files. Based on Mauricio Morales's answer, I created the following script.
with open('C:/Temp/Substitutions.txt') as f:
files = notepad.getFiles()
for file in files:
notepad.activateFile(file[0])
for l in f:
s = l.split()
editor.replace(s[0], s[1])
f.seek(0) # Reset file input stream
If you're replacing the same words in several different files all the time, recording your action once using these buttons and saving it as a macro will be helpful. *Notepad++

Is posible to add characters to a string as part of a regular expression (regex)

I use an application to find specific text patterns in free text fields in XML records. It uses regex to identify the pattern and then it is tagged in the XML. For a specific project, it would be a great time saver (I am working with about 18 million records) if I could add 2 characters 27 in front of one of the pattern I have to use.
Can this be done or am I just going to have to go the long way around?
No, you can't have a regex match text that isn't there. A regex will only be able to return text that is part of the original text.
However, if you matched into groups, you could potentially use the group name for extra information about what you're matching.
Regex is not the right tool if you'd like to edit an XML file. Instead, use a modern language like Python, Perl, Ruby, PHP, Java with a proper XML parser module. If you work in Unix like shell, I recommend xmlstarlet
That said, if you'd like to go ahead with a substitution, you can try sed (at your own risks) :
sed -i -r 's/987654/27&/g' files*.xml
(use only -i switch only to modify in-place)

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.