Re: Recognizing newline character in regular expressions
- To: mathgroup at smc.vnet.net
- Subject: [mg67580] Re: Recognizing newline character in regular expressions
- From: bghiggins at ucdavis.edu
- Date: Sat, 1 Jul 2006 05:12:13 -0400 (EDT)
- References: <e82nga$r1n$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
How about the following: mystring2 ="0.0000 11.97 -131.00 1.00 1\n0.0000 3.97 -134.00 1.00 1\n0.1597 0.00 0.0251 \ 1.00 2" Using RegularExpressions StringReplace[mystring2,RegularExpression["(.*)\n(.*)"]->"$1"~~" * "~~"$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 or more simply StringReplace[mystring2,"\n"~~y_->" * "~~y] Hope this helps. Cheers, Brian D.C.Grady at gmail.com wrote: > 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.