Sunday, 15 January 2012

bash - Regex to Insert String at Certain Position of Java Method -



bash - Regex to Insert String at Certain Position of Java Method -

i want add together throws java.lang.exception on java methods using sed.

for example,

public static void main(string[] args) {

would replaced with:

public static void main(string[] args) throws java.lang.exception {

i came regex matching java methods purposes:

(public|static|private|protected)* (void|int|string|double|float)+\s([a-za-z]*)\s*\(.*\)\s*

i know can utilize sed s/find/replace/g file replace patterns, how can match java method , insert throws java.lang.exception after closing parenthesis parameters ) , before opening curly brace { of java method?

how about

sed -r 's/((public|static|private|protected|\s)*(void|int|string|double|float|\s)+\w+\([^)]*\)\s*)/\1throws java.lang.exception / g'

test:

echo "public static void main(string[] args) { " | sed -r 's/((public|static|private|protected|\s)*(void|int|string|double|float|\s)+\w+\([^)]*\)\s*)/\1throws java.lang.exception / g'

will give output as

public static void main(string[] args) throws java.lang.exception {

regex bash sed

No comments:

Post a Comment