News for hackers. Stuff that matters.
Re:Ask HN: What's the most valuable thing you can learn in a... by swiley on Wednesday November 20, 2019 @02:23AM (#21581777)
Regex don’t do transformations? All they can do is match strings.
Re:Ask HN: What's the most valuable thing you can learn in a... by leokennis on Wednesday November 20, 2019 @03:36AM (#21582139)
If I have a list of names I’d like to reverse:

John Smith > Smith, John Anna Peterson > Peterson, Anna

I can write:

(.) (.)

And replace with:

\2, \1

Not sure if that’s part of some “official” regex spec but it works more or less everywhere.

(Of course if it was an actual list of names I’d have to be a lot more intelligent with double names etc.)

Re:Ask HN: What's the most valuable thing you can learn in a... by jeltz on Wednesday November 20, 2019 @05:52AM (#21582750)
Almost. All regular expressions do is match strings and find the starts and ends of whole matches and match groups. And this is an important distinction since match group support adds quite a bit of complexity to the theory behind regular expressions and also increases their usefulness by quite a bit.

Regular expression libraries then often can use these boundaries, for the whole match and for groups, for transform the string (e.g. replacing what was matched by a group with some other content).

Re:Ask HN: What's the most valuable thing you can learn in a... by aeternum on Wednesday November 20, 2019 @03:09AM (#21582011)
Regex grouping expressions along with find & replace enable transformations.
Re:Ask HN: What's the most valuable thing you can learn in a... by TJSomething on Wednesday November 20, 2019 @03:00AM (#21581973)
Transformation by substitution is part of most dialects of regex, in the form of capturing groups.