Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regualr Expression Help (substring search)
#1
Hi All,

Trying to get the 6th number out of this line reliably. don't quite know whether I should substring on the spaces between them (reliably consistent). The numbers will be variable, of course.


Code:
Copy      Help
RED    626.217102    820.000000    461.000000    64.519341    2636.535645    447119.000000


Thanks for any advice on strategies.

Stuart
#2
Code:
Copy      Help
str text="RED   626.217102   820.000000   461.000000   64.519341   2636.535645   447119.000000";;sameple string to be fed in by prior code

str pattern="\d{1,6}\.\d{6}";; regular expression pattern


ARRAY(str) result
findrx(text pattern 2 4 result);; puts each pattern match into a string array "result"


str volume.set(result[0 4]);; outputs the 5th (0, 1, 2, 3, 4)  element of the array into the str "volume"
out volume

double volumeint=val(volume 2);; converts the string into a double variable

double volumeCCs=(volumeint*0.001);; converts from millileters to cubic centimeters of volume
out volumeCCs



VARIANT v.round(volumeCCs 3);; rounds to 3 decimal places
volumeCCs=v
out volumeCCs
#3
thanks for posting this! i'm always needing to learn more of reg-ex but dont seem to find the resources here.
An old blog on QM coding and automation.

The Macro Hook
#4
Code:
Copy      Help
str text="RED   626.217102   820.000000   461.000000   64.519341   2636.535645   447119.000000";;sameple string to be fed in by prior code

str pattern="\d{1,6}\.\d{6}";; regular expression pattern

Between 1 and 6 digits followed by a decimal point followed by exactly 6 digits

Between 1 and 6: {1,6}
digits: \d
followed by a decimal point: \. - note that since the decimal point is a metacharacter with special meaning in RegEx that must put a \ in front of it to denote it as a "literal" decimal point

followed by exactly 6 digits: \d{6}

I am an absolute beginner at this and half the time it's only by trial and error that I get one that works but I thought I would share what I know. If there are any suggestions or improvements (or mistakes) I am making, please feel free to let me know.

I intend to read some of the resources on the internet that have exercises one can do to get the logic and tricks of RegEx down. Any good suggestions would be appreciated.






[/b]


Forum Jump:


Users browsing this thread: 1 Guest(s)