splitting a string using regex

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

splitting a string using regex

Steve Quezadas-3
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 | Threaded
Open this post in threaded view
|

Re: splitting a string using regex

Steve Quezadas-3
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 | Threaded
Open this post in threaded view
|

Re: splitting a string using regex

Carlo-2
Hi Steve

If you use \n then that is trying to match a non-printable character i.e. linefeed. The \\n will match the text '\n'.

Example matching linefeed:

> myString := 'one{1}{1}two{1}{1}' format: {String lf}.
> re := '\n' asRegex.
> myString splitOn: re.

Answers a collection of 5 elements.

Regards
Carlo

On 15 Jan 2019, at 06:25, Steve Quezadas <[hidden email]> wrote:

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