Regex code problem

Hi.
I use regexp for arduino nano.
But, code not runing. Please help me.
I defined a string named meta.
In string: "seta-1.5 nema oma hama"
I want to save the value after the seta (+ -) as float. but I could not. can you help me. code:

String meta;
if (meta.indexOf(seta) > -1) {
MatchState ms;
ms.Target(meta.c_str());
char result = ms.Match("seta[+-]?%d*%.%d+)(?![-+0-9%.]);
if (result == REGEXP_MATCHED)
{
char buf(100);
Float k = atof(ms.GetCapture(buf, 0));
Serial.println(k,1);
}

i tried it too:

char result = ms.Match("seta%d+(%.%d+))?",0);

Post a complete program using Code Tags.

Have a look at String related instructions, especially tofloat

the program is too long, but I have written as part of the experiment.
Screen capture:
photos I last tried in the code

Then make a MCVE :slight_smile:

But really, regex on a uC? Using String is already like firing a bazooka on a mosquito, regex is like firing Fat Man to kill it....

lesept:
Have a look at String related instructions, especially tofloat

not the entire string. -1.2 or +0.5 or... after the word seta as float

Of course, it was just a hint

Where's my fault? I cannot read the float value on the serial monitor. Is the pattern wrong?
Last code can be seen in the photo I uploaded

Please post your code in your message, following the rules of the forum

Sory.

I'm trying the code, the serial monitor only 'find seta' I see.
k There is no float value.
I think there is an error in pattern or code

#include .h
String meta="erdem seta-1.2 naber";
void setup ()
{
Serial.begin (115200);
}

void loop ()
{
if (meta.indexOf(seta) > -1) {
Serial.println("find seta");
MatchState ms;
ms.Target(meta.c_str());
char result = ms.Match("SETA%d+(%.%d+))?",0);
if (result == REGEXP_MATCHED)
{
char buf(100);
Float k = atof(ms.GetCapture(buf, 0));
Serial.println(k,1);
}
}

Read this: Read this before posting a programming question .... As the title says, you should have read it before posting. Read it all, but pay particular attention to Item #6. Post your code correctly using Code Tags.

Also, before posting again, auto-format your code (ctrl-t in the Arduino IDE) so that you have proper indenting.

Search for the position of 'seta' in your string add 4 and take the right part of the string beginning at that position. Then do the tofloat thing

lesept:
Search for the position of 'seta' in your string add 4 and take the right part of the string beginning at that position. Then do the tofloat thing

can you explain this? how do I write the code?

byte pos = meta.indexOf("seta") ;
This should provide the position of 'seta' in your string.

String metaend = meta.substring (pos+1) ;
This will get the last part of meta after 'seta'

float value = metaend.toFloat() ;

I wrote it without testing, you tell me...

thanks. will I try, but does the "to.Float" command support expressions that start with + -?

Don't know. Try it :

void setup() {
  Serial.begin(115200);
  String str1 = "+123.456";
  String str2 = "-123.456";
  String str3 = "abc123.456";
  Serial.println(str1.toFloat());
  Serial.println(str2.toFloat());
  Serial.println(str3.toFloat());
}

void loop() {}

output:

123.46
-123.46
0.00

And this :

void setup() {
  Serial.begin(115200);
  
  String meta = "erdem seta-1.2 naber";
  byte pos = meta.indexOf("seta") ;
  String metaend = meta.substring (pos + 4) ; // <-- modified !!
  Serial.println(metaend);
  float value = metaend.toFloat() ;
  Serial.println(value);
  
  meta = "erdem seta+1.2 naber";
  pos = meta.indexOf("seta") ;
  metaend = meta.substring (pos + 4) ;
  Serial.println(metaend);
  value = metaend.toFloat() ;
  Serial.println(value);
}

void loop() {
}

outputs:

-1.2 naber
-1.20
+1.2 naber
1.20

So the correct version is:

String metaend = meta.substring (pos+4) ;

Dear lesept,
you are awesome.
you solved my problem.
i don't need to use regex library

thank you very very much.

You're welcome.
BTW you should know that using Strings may cause memory problems if your code runs for a "long" time. I helped you in this way because you had already begun to work with Strings, but it may be a good idea to swap to c-strings, aka arrays of char

Thank you for your advice. The strings will be used occasionally (rarely) within the program. so I thought it wouldn't be a problem.?

Honestly, I don't know...