Importing a number after a string?

Hi,

How to copy a real number after a string in a text file. This number can be in any form (integer /decimal /exponential). For example

aaaa bbb (xyz / cc.dd ee) : = 3.456e2

or

aaaa bbb (xyz / cc.dd ee) : = 345.6

or

aaaa bbb (xyz / cc.dd ee) : = 345

I need a function, which can copy the numerical value after “aaaa bbb (xyz / cc.dd ee) : =” irrespective of its form.

Thanks for the help.

This thread should help. That post has some suggestions for simple formatting. To get really good formatting, I have a “one line” external function that calls the C function, sprintf. I can repost it here if you are interested.

Regards

How about

extract[s_] := 
 s // StringSplit[#, "aaaa bbb (xyz / cc.dd ee) : = "] & // First // 
  Read[StringToStream[#], Number] &

s1 = "aaaa bbb (xyz / cc.dd ee) : = 3.456e2"
s2 = "aaaa bbb (xyz / cc.dd ee) : = 345.6"
s3 = "aaaa bbb (xyz / cc.dd ee) : = 345"

extract /@ {s1, s2, s3}
(* {345.6, 345.6, 345} *)

Appreciate your help. I went through the post and the other one mentioned there. Not understood all the steps yet, but my problem is more of copying a numerical value after a string in the text file, rather than changing its accuracy or format. I am bit confused now, whether I need to convert the string to CForm for Mathematica to read it in the text file. However, my previous experience is that Mathematica can read a string in a text file, as it is. Please correct me if I am wrong.

In my case, it would be fine if I am able to copy a number in its given form, as long as it is of correct numerical value for all possible formats. For example, if the number is 1.23e2 in the text file, I am not worried whether I get the output as 1.23e2 or 123 or 123.00 or 1.23*10^2 etc.

Hope it explains my problem little better. Thanks once again.

Hope it explains my problem little better

Not really. 1.23e2 is not a valid format for a number in WL. 1.23*^2 or 1.23*10^2 are valid. The code I provided converts the invalid form to the valid one and the result is a number, not a string.

If you want a string result then just split the string

StringSplit[#, "aaaa bbb (xyz / cc.dd ee) : = "] & /* First /@ {s1, s2, s3}
(* {"3.456e2", "345.6", "345"} *)

Basically the same as Rohits last post, but without need to specify the leading part of the source strings:

s1 = "aaaa bbb (xyz / cc.dd ee) : = 3.456e2";
s2 = "pqr : = 345.6";
s3 = "stu / (v w x) : = 345";

Last /@ StringSplit[{s1, s2, s3}]
(* {"3.456e2","345.6","345"} *)

Thanks. It worked for any kind of numerical value. However, if the i/p has more than one strings present, this method does not work. For example if

s4="pqr : = 3
 aaaa bbb (xyz / cc.dd ee) : = 3456e2";
extract /@ s4

error message is -

Read::readn: Invalid real number found when reading from StringToStream[pqr : = 5].

This point is important as I have to use this method to extract a parameter from a text file having large number of strings, each followed by a number.

Thanks for your help once again..

Well, now you have changed the requirements. It would have been better if your original question stated the requirements clearly and completely.

In the s4 example there is a newline \n character separating the two cases. Is that always true or can s4 be like this?

s4 = "pqr : = 3 aaaa bbb (xyz / cc.dd ee) : = 3456e2";

or

s4 = "pqr : = 3aaaa bbb (xyz / cc.dd ee) : = 3456e2";

Is the number always preceded by : = or are there other possible character sequences?

If there is always a newline and always preceded by : = then

(s4 // StringSplit[#, " : = " | "\n"] &)[[2 ;; ;; 2]]
(* {"3", "3456e2"} *)

That will extract the numbers as strings, you can convert to numeric by mapping

Read[StringToStream[#], Number] & /@ {"3", "3456e2"}
(* {3, 345600} *)

s5 = "pqr : = 3
 aaaa bbb (xyz / cc.dd ee) : = 3456e2
abc : = 345.6";

(s5 // StringSplit[#, " : = " | "\n"] &)[[2 ;; ;; 2]]
(* {"3", "3456e2", "345.6"} *)

Well, now you have changed the requirements. It would have been better if your original question stated the requirements clearly and completely.

Actually, I mentioned that I need to copy a number from the text file, after a particular string. Anyways, finally, StringSplit worked, however I had to make some changes to what you suggested. Thanks for the help.