Regular Expression #8

  • Follow


Hi;
I have a string which likes: 
Test: A, B, C
A , B , C are a float number with different length.

I want to extract value A.
Since the start ":"  and end "," are clear. I wrote my code like:


idn1 = fscanf(s1)    %idn1 is data coming from COMport
str= idn1;
pat = '\w*\:\w*\,'; % this is a pattern for finding number A
t = regexp(str, pat, 'match')



Goal is to save the value of A is 100 iteration in a file.
But my pattern is wrong and it gave me an empty output!
Do you have an idea? 


Thanks for help
0
Reply sb 11/23/2010 8:41:22 PM

On 10-11-23 02:41 PM, sb bk wrote:

> I have a string which likes: Test: A, B, C
> A , B , C are a float number with different length.
>
> I want to extract value A.
> Since the start ":" and end "," are clear. I wrote my code like:
>
>
> idn1 = fscanf(s1) %idn1 is data coming from COMport
> str= idn1;
> pat = '\w*\:\w*\,'; % this is a pattern for finding number A
> t = regexp(str, pat, 'match')
>
>
>
> Goal is to save the value of A is 100 iteration in a file.
> But my pattern is wrong and it gave me an empty output!
> Do you have an idea?

You should not use \ before the colon or comma. It would also be better to use 
+ instead of * as the repetition specifier.

But what I would do is

textscan(s1, '%*s%f%*[^\n]')

You do not need to put the colon in to the format as it will be considered 
part of the first string. You do not need to put the comma in to the format 
because it is not a valid character in a floating point number so that reading 
of the floating point number would stop at the comma, and the comma would then 
be absorbed by the final format specifier that will eat everything to the end 
of the line.
0
Reply Walter 11/23/2010 8:53:35 PM


1 Replies
296 Views

(page loaded in 0.04 seconds)

Similiar Articles:













7/26/2012 12:30:38 AM


Reply: