6
$\begingroup$

I want to replicate the following code using RegularExpression instead of StartOfLine:

In[1]:= StringSplit["line1\nline2\nline3",StartOfLine]//InputForm

Out[1]={"line1\n", "line2\n", "line3"}

Failed Attempt:

In[34]:= StringSplit["line1\nline2\nline3",RegularExpression["^/m"]]//InputForm

Out[34]= {"line1\nline2\nline3"}

How can I get this to work using Mathematica's flavor of regex?

$\endgroup$

1 Answer 1

7
$\begingroup$

In Mathematica, you set the Regex options with prefix notation.

For multiline mode, you need to prepend your regex expression with "(?m)"

This is in the Details section of RegularExpression:

enter image description here

StringSplit["line1\nline2\nline3", RegularExpression["(?m)^"]] // InputForm
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.