Suppose that I want to replace lines containing "b" with a new "XXX" line. This works as expected with regular expressions:
StringReplace["a\n_b_\nc", RegularExpression["(?m)^.+b.+$"] -> "XXX"]
"a
XXX
c"
The (?m)
activates multi-line mode so that ^
and $
can refer to each of the line beginnings and ends in the string. This is kind of cryptic so I prefer to use string expressions, but then, I get a different result:
StringReplace["a\n_b_\nc",
Shortest[StartOfLine ~~ ___ ~~ "b" ~~ ___ ~~ EndOfLine] -> "XXX"]
"XXX
c"
As a workaround, I can make it work like this:
StringReplace["a\n_b_\nc",
Shortest["\n" ~~ ___ ~~ "b" ~~ ___ ~~ "\n"] -> "\nXXX\n"]
"a
XXX
c"
but I would think that one should be able to use StartOfLine
and EndOfLine
.
What is going on? Are StartOfLine
and EndOfLine
incompatible with Shortest
?