From: Matt Brooks Date: 2009-10-30T01:25:08+09:00 Subject: Re: String Matching Problem Okay, thanks for asking. I don't really know why it stops working with line[/\A#{cmd}(\z|,)/], basically my program timesout on a message when it thinks it doesn't receive it. And that is what happened after about 6 messages when I used that approach. Some background. It is a network interface of sending ascii messages to a machine which then sends certain ascii messages back, with some fields of the message known, some unknown till it returns. I can't simply send the message and wait for it to return, because I have multiple threads sending different messages at the same time, therefore, the order in which response messages return is unknown, so they just go into an array of size 600, one message per index. Each message is terminated with a newline, \n. The order of operations it usually psuedo code, SEND(CHLE,23,4) GETMESSAGE(CHLE,23). Because, CHLE,23 is all I am certain that will return for that message, so i look for that. Therefore this pseudo GETMESSAGE function reads from the tcp port I opened up earlier, 1 character at a time, loading each into a temp string, until a \n is seen, then it loads that string into the buffer[0] spot, then checks if it happens to match the message I was waiting for anyway, sometimes it does, in that case it returns that message.(Perfect scenario, done, move on to sending next message...) Other times it does not match, like when another thread sent a different message and it was that threads return message, therefore it is just thrown in the buffer, so later it can be found by that other thread. And Immediately I search the whole buffer for a match o my message I am looking for(what if the response was actually already in the buffer from the other thread writing it previously into the buffer), so i look through the buffer using the line[cmd] way. If it does NOT find it, I go back to reading one char at a time until a \n is seen. If it does find it, it returns it(like the perfect scenario earlier). Since after all the above happens, Then presumably the other thread gets it chance to find its return message, it reads till a \n, throw it in buffer, checks if matches, if does awesome good timing, and would be done. However, usually not a match right away so then looks through buffer, because chances are the 1st thread wrote it in the buffer already while it was looking for its specific response message. This all works perfectly, except from time to time I get a false positive on a match when using line[cmd]. When for example I am looking for specifically "CHLE,23" and "CHLE,231" actually is returned. I want CHLE,23,3,2,3,4,5,3,2 to say YES a match if I was looking for "CHLE,23" but if CHLE,231,3,2,3,4,5,3,2 is returned and I was looking for "CHLE,23" I want it to say NO not a match. Also, incase something screws up with machine not returning a message, after 10 seconds of not responding, it just returns an error message that says Response never received for 10 seconds. This was what was happening when I tried each of the methods suggested in this thread... Therefore I was thinking maybe they are hogging resources and the network buffer is filling up and missing messages while I am doing a resource intensive operation on a buffer of 600 items... all theory though, I just know line[cmd] way works, again except for false positive. Hopefully that makes sense, It is a little complicated which is why initially I didn't go into why I have a buffer and all... Ideas on how to prevent my false positive situation when using line[cmd] way? Thanks Again!!! -Matt -- Posted via http://www.ruby-forum.com/.