0

I would like to capture multiple strings that occur in any order into a single capture group in sed.

For instance

sed -i '' -E -e "s/(public |static |volatile |transient |final |protected |private )+[[:blank:]]([[:alnum:]])[[:blank:]]([:alnum:]])[[:blank:]]\(([[:alnum:]\,\s]+)\)/\1 func \3\4 -> \2/g"

I have a capture group in () and I have the + sign to signify one or more occurrences, but this syntax creates one or more capture groups whereas I want a single capture group with one or more strings.

I would like sed to greedily capture one or more occurrences of the specified strings in any order into a single capture group.

For instance if I have

private static Object Test (boolean t)
public transient volatile Object Test (int k)

I would like to replace it with

private static Test (boolean t) -> Object
public transient volatile Test(int k) -> Object

As you might guess by now it is a rudimentary sed script that converts some java syntax to swift equivalent.

nishant
  • 736
  • 1
  • 12
  • 22

2 Answers2

2

You can wrap your initial group into outer group.

In this case \1 will refer to new outer group (as it starts first).

sed -E 's/((public |static |volatile |transient |final |protected |private )+)/\1func /g'

Exmaple of output for your input:

private static func 
public static func 
static transient func 
static volatile func 
markalex
  • 8,623
  • 2
  • 7
  • 32
  • This helped me move forward, but it gets stuck on the rest of the capture group numbering, I put in the full script into the question to explain what the following groups need to do. Is there a logic to how the numbering will be for the inner ()? – nishant Apr 02 '23 at 00:12
0

I have selected @markalex's answer as the correct answer because it let me move forward. For others that might be interested in the full sed script - I have pasted it below.

declare -a typePrefix="public[[:blank:]]+|static[[:blank:]]+|volatile[[:blank:]]+"
declare -a jtype="[[:alnum:]_]*"
declare -a func="[[:alnum:]_]*"
declare -a params="[[:alnum:]\:\,\ ]*"
sed -i '' -E -e "s/(($typePrefix)+)[[:blank:]]*($jtype)[[:blank:]]*($func)[[:blank:]]\(($params)\)/\1 func \4(\5) -> \3/g" $1
nishant
  • 736
  • 1
  • 12
  • 22