1

~I have a client which is sending the a message to my server and I am trying to get substrings in order to extract them into variables. I want to use regex for this. Although I have no syntax problems, it will not match. This is the message I am sending and my code.

" PUT /John\r\n\r\n London "

private StreamReader sReader = null;
private StreamWriter sWriter = null;


public SocketClass(Socket s)
    {
        socket = s;
        NetworkStream nStream = new NetworkStream(s);
        sReader = new StreamReader(nStream);
        sWriter = new StreamWriter(nStream);
        startSocket();
    }
String txt = "";
        while (sReader.Peek() >= 0)
        {
            txt += sReader.ReadLine() + "\r\n";
        }

        else if (txt.Contains("PUT"))
        {

            Console.WriteLine("triggered");
            Regex pattern = new Regex(@"PUT /(?<Name>\d+)\r\n\r\n(?<Location>\d+)\r\n");
            Match match = pattern.Match(txt);
            if (match.Success)
            {
                String Name = match.Groups["Name"].Value;
                String Location = match.Groups["Location"].Value;
                Console.WriteLine(Name);
                Console.WriteLine(Location);

            }
        }
20
  • 2
    If you want to match newline/CR characters, pass RegexOptions.SingleLine as a parameter to the Regex constructor. Commented Feb 20, 2017 at 11:57
  • I have added the parameter but it still does not match Commented Feb 20, 2017 at 12:01
  • You need to prefix some characters. Like the / to // Commented Feb 20, 2017 at 12:03
  • 1
    Your input string does not have (a) \r\n at the end and (b) will not match against \d+ which means one or more digits. Commented Feb 20, 2017 at 12:03
  • 1
    You input string has spaces between LF and CR chars that aren't matched in the regex. This question is bogus. Commented Feb 20, 2017 at 12:07

1 Answer 1

1

The problem seems to be that while your input has alphanumeric characters your regex is looking for \d which are numeric digits. The regex can be easily changed to this to make it work:

Regex pattern = new Regex(@"PUT /(?<Name>.+)\r\n\r\n(?<Location>.+)\r\n");

. represents any character. It may be that you could narrow it down more to say the match has to be alphabetic characters or something else but the above will certainly work for your given input.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply, I firstly tried to replace the whole line with PUT /(?.+)\r\n\r\n(?.+)\r\n but this threw me an exception. Then I tried to replace the "d"s with "."s which fixed the exception but it still wouldnt match
I actually fixed it. Thank you so much !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.