I am answering my own question because I found the solution for it.

This is the code that didn't work:
> myString := 'one\n\ntwo\n\n'.
> re := '\n\n' asRegex.
> myString splitOn: re.

The reason it didn't work was because you apparently have to escape the newlines pattern in the regex line. So the correct (working) example is here:
> myString := 'one\n\ntwo\n\n'.
> re := '\\n\\n' asRegex.
> myString splitOn: re.

I am putting this on here just in case someone else runs into the same problem.

- Steve



On 01/14/2019 09:06 AM, Steve Quezadas wrote:
I am trying to split a string in pharo using a regular expression.

A simple example that works:
myString := 'one\n\ntwo\n\n'.
myString splitOn: '\n\n'.

A simple example that does not work:
myString := 'one\n\ntwo\n\n'.
re := '\n\n' asRegex.
myString splitOn: re.

The result of the above is I get the regular old string back ('one\n\ntwo\n\n'). I went through the source code and it should be able to handle a regex object:
 > "splitter - can be a subsequence, a Block or a Regex (String receiver
 > only). Any other object used as a splitter is treated as an Array
 > containing that object."

I am baffled as to why it's not working. is there something simple I am missing?

- Steve


Reply via email to