2

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.

user3528086
  • 73
  • 1
  • 1
  • 4
  • Please provide specific examples – sshashank124 Jun 28 '14 at 05:56
  • @sshashank124 - example can be any two sets of regular expressions. Like find- (http://something.com/somedir1548/) and replace with- (index45872.html). Then Find (http://something.com/somedir8493/) and replace with- (k2548/index45872.html), like so on with many different search and replace pairs. – user3528086 Jun 28 '14 at 06:16
  • Is there is something in which we can chain up (find) of many different regular expressions in one (perhaps by use of delimiters or else). – user3528086 Jun 28 '14 at 06:28
  • I got a thing related to mine at- [this](http://stackoverflow.com/questions/11389466/multiple-word-search-and-replace-in-notepad/16104946#16104946) in 'AdrianHHH' answer. Will try this later. – user3528086 Jun 28 '14 at 07:13

2 Answers2

1

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)
zx81
  • 41,100
  • 9
  • 89
  • 105
  • I just want expression way of regular expression . Dont concentrate or reply on 'tool' word. (And is regular expression not a programming?) – user3528086 Jun 28 '14 at 06:00
  • I have some knowledge of Java – user3528086 Jun 28 '14 at 06:20
  • Great! Added Java section to the answer. :) – zx81 Jun 28 '14 at 06:25
  • the second parameter 'myreplacement' of replaceAll(..) is also treated as regex or it is a simple String? – user3528086 Jun 28 '14 at 06:48
  • It's a "replacement string": It could be a literal string, like `"dog"`, or also contain text that was captured by capture groups. For instance, `$1` would refer to the text captured by `(the first set of parentheses)` This allows you to build an elaborate match. Then you can build a more complex replacement functions (a "lambda"), in the middle of which you can do whatever you like—check a database for instance. For an example of a simple replacement lambda, see [this code](http://www.rexegg.com/regex-best-trick.html#javacode) – zx81 Jun 28 '14 at 07:14
  • Thanks for details. In addition to writing an application and also not going through paid applications, we can do this thing in n++ like '[Here](http://stackoverflow.com/questions/11389466/multiple-word-search-and-replace-in-notepad/16104946#16104946)'. I have tried and tested this way but also go through writing an application way! – user3528086 Jun 29 '14 at 09:10
0

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612