MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Recognizing newline character in regular expressions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg67537] Recognizing newline character in regular expressions
  • From: "D.C.Grady at gmail.com" <D.C.Grady at gmail.com>
  • Date: Fri, 30 Jun 2006 04:14:09 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

I'm having trouble getting Mathematica to find the newline character in
an input string.  For example,

dataString = "    0.0000      0.00 0.0000 1.00 2
    0.0000      11.97 -131.00 1.00 1
    0.0000      3.97 -134.00 1.00 1
    0.1597      0.00 0.0251 1.00 2
    0.1597      11.97 -130.00 1.00 1
    0.1597      3.97 -134.00 1.00 1
    1.4861      19.47 0.0000 1.00 -1
    2.7083      0.00 0.3393 1.00 2
    2.7083      11.97 -127.00 1.00 1";

I would like to write a function that would take a set of data values
as a string in this sort of format and replace all the line breaks with
some other string, say "*".  So I tried

In[53]:=
StringReplace[dataString,RegularExpression["\n"]\[Rule]"*"]

Out[53]=
    0.0000      0.00 0.0000 1.00 2     0.0000      11.97 -131.00 1.00 1
    \
0.0000      3.97 -134.00 1.00 1     0.1597      0.00 0.0251 1.00 2
0.1597 \
     11.97 -130.00 1.00 1     0.1597      3.97 -134.00 1.00 1
1.4861      \
19.47 0.0000 1.00 -1     2.7083      0.00 0.3393 1.00 2     2.7083
11.97 \
-127.00 1.00 1

which unfortunately don't to work.  I've also tried matching

RegularExpression["\\n"]
EndOfLine
StringExpression[\[NewLine]]

none of which seem to work.  The problem appears to be that Mathematica
is automatically stripping newline characters out of the input string;
this occurs even if I use the delayed evaluation assignment

dataString:= ...

Does anyone have any ideas?  What's particularly aggravating is that if
you look at the cell expression for the dataString cell, you can see
that there are "\n" characters present in the string, and using this
same approach works on the data with no problems when using a text
processor that supports regular expressions.


  • Prev by Date: RE: Change Symbol in ListPlot
  • Next by Date: Re: Change Symbol in ListPlot
  • Previous by thread: Options to FunctionInterpolation
  • Next by thread: Color names and the 10 elementary colors?