Issue
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.
Solution
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
Answered By - markalex Answer Checked By - David Goodson (WPSolving Volunteer)