jmporterog / maven-replacer-plugin

Automatically exported from code.google.com/p/maven-replacer-plugin
MIT License
0 stars 0 forks source link

Extension to regex support #24

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Thanks for this plugin!  A much needed capability that I'm surprised maven
didn't ship with.

Regex matching is really useful, but being able to reference portions of
the regex in your substitution string would make it nearly unstoppable. 
For example, in perl:

s/beginning\(.*\)middle\(.*\)end/b\2m\1e/g

will not only take the strings "beginning", "middle" and "end" and replace
them with b, m, and e (only if they occur in the same line), it will also
take the two series of characters that match the expressions between them
and transpose them according to the replacement string, so:

beginning-stuff-middle-here-end 

will become:

b-here-m-stuff-end

This is a contrived example, but I've found this capability to be insanely
powerful in vi, and supporting it here would allow me to do the same thing
automatically in a project.

Original issue reported on code.google.com by curtis.l...@gmail.com on 21 Jan 2010 at 1:05

GoogleCodeExporter commented 9 years ago
Thanks for the suggestion, I will look into it for the next release.

Steven

Original comment by baker.st...@gmail.com on 21 Jan 2010 at 2:04

GoogleCodeExporter commented 9 years ago
I am having trouble implementing this feature. I am also having trouble finding 
any
existing solutions in Java for this (even anything outside of Vim).

If you could help me implement this or point me in the direction of a useful 
document
on implementing this, it would be greatly appreciated.

Otherwise, this will likely not make it for the next release.

Original comment by baker.st...@gmail.com on 3 Feb 2010 at 1:46

GoogleCodeExporter commented 9 years ago
If you pulled it off, you'd be the first java implementation I know of.  I 
ended up
handling the need for this in my own project by using the maven-exec-plugin to
offload the work to perl.  I wonder if it would be possible to pull portions of 
the
Perl source (http://www.cpan.org/src/README.html), port them into Java, and use
those?  I'll let you know if I get inspired and have any luck with this.

Original comment by curtis.l...@gmail.com on 3 Feb 2010 at 2:28

GoogleCodeExporter commented 9 years ago
I gave perl-5.11.4.tar.gz from above CPAN site a quick look, and my gut feeling 
is
that the good stuff is in regexec.c.  A comment at the beginning of the file 
seems to
back that up :).  I'm afraid my C is far too rusty to really make heads or 
tails of
it, though.

Original comment by curtis.l...@gmail.com on 3 Feb 2010 at 2:38

GoogleCodeExporter commented 9 years ago
Or, actually it looks like this might be supported in java's regex stuff 
already:
http://www.exampledepot.com/egs/java.util.regex/Group.html

The exciting bit of code is this one:
{code}
CharSequence inputStr = "abbabcd"; 
String patternStr = "(a(b*))+(c*)"; 

// Compile and use regular expression 
Pattern pattern = Pattern.compile(patternStr); 
Matcher matcher = pattern.matcher(inputStr); 
boolean matchFound = matcher.find(); 
if (matchFound) 
{ 
// Get all groups for this match for (int i=0; i<=matcher.groupCount(); i++) { 
String
groupStr = matcher.group(i); 
} 
} 
{code}

Original comment by curtis.l...@gmail.com on 3 Feb 2010 at 3:00

GoogleCodeExporter commented 9 years ago
I'm not quite sure how the above code can help. I think I will need to parse 
the token 
into sub replacement tokens for this to take place.

Due to the complexity of this enhancement, it has not made the 1.3 release. I 
will 
continue to investigate this enhancement.
Otherwise, you are most welcome to solve this enhancement and submit the change.

Original comment by baker.st...@gmail.com on 9 Feb 2010 at 10:27

GoogleCodeExporter commented 9 years ago
The String.replaceAll() method should do the job (however, with a slightly 
differnt syntax):

public class RegexpReplaceTest {

  public static void main(String[] args) {
    String regexp = "beginning(.*)middle(.*)end";
    String replaceString = "b$2m$1e";

    String testString = "beginning-stuff-middle-here-end";

    // Prints "b-here-m-stuff-e"
    System.out.println(testString.replaceAll(regexp, replaceString));
  }
}

In Java regular expressions, back referencing is done using a "$" symbol and 
there is no need to escape the braces.

Original comment by st.fer...@gmail.com on 9 Jun 2010 at 4:43

Attachments:

GoogleCodeExporter commented 9 years ago
I can confirm that the above works within this plugin with the above's 
variables,
token: regexp
value: replaceString
file contents: textString

Thanks for the help st.ferstl.
Since this is already functional I will close this Issue.

Original comment by baker.st...@gmail.com on 13 Jun 2010 at 1:09