I am trying to parse rsync command line options that start with either a -
or --
. Some of the --
parameters can also be assigned a value using =
. The last two parameters in the example have no option names. They represent the Source and Destination folders.
So here is a literal generic example of the parameters of rsync:
rsync -av -t --out-format="%M %'10'b %f" --ignore-times -i --exclude-from = /home/user1/myapppath/exclude.txt --ignore --link-dest="/home/user1/myback2/abc/2024-09-11T16_55_45" source/abc dest/abc
I found this regex in an old online post (--(?<option>.+?)s+(?<value>.(?:[^-].+?)?(?:(?=--)|$))?)+?
but this does not handle -
and also the tail end parameters. I am trying to adapt it. I can handle the last two separately in code if necessary. I am using the PCRE2 implementation. Thanks!
https://regex101.com/r/Gtb1yH/1
4
For the example string and the separate parts that you want to match from it, you might use:
(?<!S)--?(?<option>[^s-][^s=]*)(?:s*=s*(?<value>"[^"]*"|S+))?|(?<other>S+)
The pattern matches:
(?<!S)
Assert a whitespace boundary to the left--?
Match 1 or 2 times-
(?<option>
Named groupoption
[^s-][^s=]*
Match a single non whitespace char other than-
followed by optional non whitespace chars other than=
)
Close the group(?:
Non capture group to make the whole part optionals*=s*
Match=
between optional whitespace chars(?<value>
Named groupvalue
with 2 alternatives"[^"]*"|S+
Match either"..."
or 1+ non whitespace chars
)
Close the group
)?
Close the group and make it optional|
Or(?<other>S+)
Named groupother
matching 1 or more none whitespace chars
See a regex demo.
Note that the last group (?<other>S+)
in this case matches the source and dest part which are at the end of the example string.
But for this part to match, they don’t have to be explicitly at the end. This part is an alternative in case the first part of the pattern with option and value do not match. It would also match if source and dest are at the beginning as they are 2 strings that consist of 1 or more non whitespace characters.
So this pattern is a best effort for the given example, which you could extend of course.
2