Parse Serial string elements to vars or array elements?

Hello,
I’ll be using an Arduino to control a third party audio DSP via a Telnet session… i.e. DSP is the Telnet Server, Arduino is client.
Got the telnet part working in a basic live Serial.read() & Serial.print() functionality, so I’m not too worried about that part.

Where I am worried is when it comes to parsing the serial responses received from the Telnet server.
I don’t have any control of the format of the messages coming back on the server…here’s what they look like:

valueIs “controlname” -20.0dB 0.80

Explained below…

valueIs - all of the lines I care about will begin with this

“controlname” - the name of the control I’m dealing with, i.e. “volume” or “mute”. It is always inside double quotes, which is where I’m mostly getting hung up I think.

-20.0dB - the current value of the control. I will ultimately need to strip off the trailing dB but I’m not thinking about that right now. Depending on the control type, this could also return as a boolean true / false.

0.80 - the current physical position of the control. I don’t really care about this right now.

So, the messages are always made of four elements separated by spaces, and the control name is always wrapped in " ".

Being a relative Arduino newbie, for the past couple of days I’ve (foolishly?) been wrapping my head around regex, and finally worked out a statement that would give me the two values I want (which really made my day):

(?<=\bvalueIs\s)"(\w+)"\s([^dB]*)

only to realize that the Arduino doesn’t support a standard version of regex… Is that true?

Then I played a bit with the library I found here :

but it doesn’t seem to be able to handle the double quotes surrounding the “controlname” that will be in my incoming string.

Ultimately I guess I’d like a string array containing the control name in index 0 and the value in index 1.

I’d also like it to be agnostic to the actual control name, to allow this code to be used easily when / if control names change.

Perhaps I’m totally overlooking a really easy way to handle this?
Pleases set me straight if it’s needed.

Thanks for reading this far.
-d

The String functions below provide fairly simple ways to parse captured Strings.

http://arduino.cc/en/Reference/StringObject

The way I'd approach the problem is to buffer the message in a c-string (null-terminated char array) and then use strtok(), strchar(), sscanf() and related functions as described in http://www.nongnu.org/avr-libc/user-manual/group__avr__string.html and http://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html to validate the content of the buffer and extract the data you want. The Arduino IDE makes all the functions of the AVR 'C' runtime library available and they provide a pretty comprehensive implementation of the standard 'C' runtime API.

The various String methods Zoomcat refers to provide a different way to access the underlying c-string functions, and you can use the String class or c-strings equally easily. Zoomcat and I have disagreed repeatedly about whether the String class is a good idea or not, so you're going to get conflicting advice here. My view is that the String class does not make the job any easier or more efficient, and introduces a reliance on dynamic memory which is undesirable on a platform with so little memory available for the heap and such limited capability to manage that heap. On versions up to 1.0.4 the String class also exposed a bug in the Arduino's heap management code which caused memory corruption; this is fixed in later versions but the String class still exposes the risk of memory fragmentation. The String class is simply not a good idea in an Arduino environment and I suggest you forget it exists - despite Zoomcat's recommendation that you use it. Everything you're trying to do can be achieved using the standard 'C' functions for string (lowercase 's') manipulation.

Everything you’re trying to do can be achieved using the standard ‘C’ functions for string (lowercase ‘s’) manipulation.

The biggest problems with c-string is that the people that promote its use never actually post any relevant working code (maybe c-string usage is not so simple). Below is a simple working example of using String functions to capture a string of characters, then extract the desired values for use in controlling two servos.

// zoomkat 12-13-11 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 1.0
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550
// use serial monitor to test

#include <Servo.h> 
String readString, servo1, servo2;
Servo myservo1;  // create servo object to control a servo 
Servo myservo2;

void setup() {
  Serial.begin(9600);
  myservo1.attach(6);  //the pin for the servo control 
  myservo2.attach(7);
  Serial.println("two-servo-test-1.0"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(3);  //delay to allow buffer to fill 
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    } 
  }

  if (readString.length() >0) {
      Serial.println(readString); //see what was received
      
      // expect a string like 07002100 containing the two servo positions      
      servo1 = readString.substring(0, 4); //get the first four characters
      servo2 = readString.substring(4, 8); //get the next four characters 
      
      Serial.println(servo1);  //print to serial monitor to see parsed results
      Serial.println(servo2);

      int n1 = servo1.toInt();
      int n2 = servo2.toInt();

      Serial.println("the numbers are :");
      Serial.println(n1);  //print to serial monitor to see number results
      Serial.println(n2);
            
      myservo1.writeMicroseconds(n1); //set servo position 
      myservo2.writeMicroseconds(n2);
    readString="";
    servo1="";
    servo2="";
  } 
}

zoomkat: (maybe c-string usage is not so simple)

That is a ridiculous suggestion. The c-string functions have been the bread and butter of string manipulation in 'C' and C++ for decades and are extremely well known and well documented. The problematic String class is built on top of them.