Wednesday, May 8, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 3415  / 1 Year ago, sat, may 27, 2023, 1:29:13

Consider this command:



echo "string.with.dots" | sed 's/(.*).(.*)/1
2/'


(Matches into a first capturing group any character until the last . and into a second capturing group any character after it.)



This outputs:



string.with
dots


Reasonably (I think) I tought that using anchors in the right combination would have managed to reverse such behavior (i.e. the match would have been string for the first capturing group and with.dots for the second capturing group), but:



echo "string.with.dots" | sed 's/^(.*).(.*)/1
2/'
echo "string.with.dots" | sed 's/^(.*).(.*)$/1
2/'
echo "string.with.dots" | sed 's/(.*).(.*)$/1
2/'


All output:



string.with
dots


I don't know how the pattern matching is implemented, but it seems that it always privileges the patterns closer to the start of the string rather than those closer to the end of the string (despite any present ^ or missing $).



How can this behavior be changed (i.e. not how to write an hard-coded solution to this example, but how to reverse the pattern-matching priority order into sed or into regexes in general), if possible?


More From » sed

 Answers
6

Add two rev and swap 1 and 2:



echo "string.with.dots" | rev | sed 's/(.*).(.*)/2
1/' | rev


Output:




string
with.dots

[#20632] Monday, May 29, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lassrake

Total Points: 400
Total Questions: 103
Total Answers: 98

Location: Netherlands
Member since Mon, Jun 22, 2020
4 Years ago
;