2

I am new with Regex and I have almost got it working. I think I make a small noob mistake and I have already looked through documentation.

I have a little script that I run twice, each time for a different string. The first string does what I want. The second doesn't. Somehow it seems that the old value of the first run is placed in front of the values after the second run. It is best to explain when I show my code and output.

Original script where a TCPclient callback should first give BS=63 and then RLs=1.

private void Main()
{
    EditString(TCPclientCallback1);
    EditString(TCPclientCallback2);
}

private void EditString(string response)
{
    Regex rgxLetters = new Regex(@"[\d]");
    Regex rgxNumbers = new Regex(@"[^\d]");
    Console.WriteLine(response);
    Console.WriteLine(rgxLetters.Replace(response, ""));
    Console.WriteLine(rgxNumbers.Replace(response, ""));
    Console.WriteLine();
}

output

BS=63
BS=
63

RLs=1
RLs=
631

The last value needs to be 1 instead of 631. In the next example I manually insert the desired string.

private void Main()
{
    EditString("BS=63");
    EditString("RLs-1");
}

private void EditString(string response)
{
    Regex rgxLetters = new Regex(@"[\d]");
    Regex rgxNumbers = new Regex(@"[^\d]");
    Console.WriteLine(response);
    Console.WriteLine(rgxLetters.Replace(response, ""));
    Console.WriteLine(rgxNumbers.Replace(response, ""));
    Console.WriteLine();
}

output. This is the desired output

BS=63
BS=
63

RLs=1
RLs=
1

That would mean that there is no issue in the regex part. In my application I get my response from the callback of a TCP client. So somehow that callback is not purely BS=63 or RLs=1

EDIT2

The callbacks of my TCP client were wrong... The Regex part is working just fine. So my question ended up to be irrelevant. Sorry for that!! Thanks anyways for all the quick responses :)

Lauran
  • 73
  • 1
  • 1
  • 9
  • Possible duplicate of [string.Replace (or other string modification) not working](https://stackoverflow.com/questions/1948978/string-replace-or-other-string-modification-not-working) – Liam Aug 14 '18 at 09:20
  • Can you write the code you use to call `EditString`, or just confirm that you get the same bad result if you call `EditString("BS=63")` and then `EditString("RLs=1")`? – Rawling Aug 14 '18 at 09:24
  • 1
    Can't reproduce, at least not by doing `EditString("BS=63"); EditString("RLs=1");`. Works as it should, second call outputs `RLs=1` / `RLs=` / `1`. – Peter B Aug 14 '18 at 09:28
  • 1
    **Could it be** that your second input string actually contains `"RLs=6^H3^H1"` (with ^H being the backspace character), or something similar? Output for `EditString("RLs=6" + (char)8 + "3" + (char)8 + "1");` is as you wrote it! – Peter B Aug 14 '18 at 09:32
  • @PeterB O.O good spot if that's the case. I'd love to know what bit of the program is capturing literal `\b`s if so. – Rawling Aug 14 '18 at 09:37
  • @PeterB According to the documentation of my device I should get `RLs=1`. I don't know if the string contains any more characters. Maybe I could write some code that rewrites the response value to get a 'clean' string again? – Lauran Aug 14 '18 at 09:41
  • Please post a [mcve], right now it's hard to know if parts of your code is example of working code or broken code. – Lasse V. Karlsen Aug 14 '18 at 09:59
  • @LasseVågsætherKarlsen yes it becomes very vague now indeed. I will be more clear – Lauran Aug 14 '18 at 10:00
  • Tried `EditString(Regex.Replace(response, @"\p{Cc}+", ""));` yet? – Wiktor Stribiżew Aug 14 '18 at 10:05
  • @WiktorStribiżew Yes I have just tried it. It places a `%` character in front of my string. I have used it likes this. `Regex rgxOther = new Regex(@"\p{Cc}+"); string dd = rgxOther.Replace(response, ""); Console.WriteLine(dd);` Output `%BS=63` and `%BS=63RLs=1`. – Lauran Aug 14 '18 at 10:10

1 Answers1

1

Your string seems to contain backspace characters, because printed output for

EditString("RLs=6" + (char)8 + "3" + (char)8 + "1");

is exactly as you wrote it:

RLs=1
RLs=
631

If you want to filter out these characters you can use this as the first step:

response = response.Replace("\b", "");
Peter B
  • 22,460
  • 5
  • 32
  • 69
  • As you've pointed out "seems to contain", we cannot be sure about that and this type of questions are primarily opinion based or just a typo, code fixes which are forbidden here on StackOverflow. – mrogal.ski Aug 14 '18 at 09:40
  • Seems more likely it'd be `63\b\b1` - typed the whole previous number in, then corrected it. – Rawling Aug 14 '18 at 09:42
  • 1
    Thank you for your answer. Unfortunately it does not solve the issue – Lauran Aug 14 '18 at 09:46
  • Oh, if you just replace the `\b` you'll just get the `63` showing up in the string. You'd need [a processing step that *applies* the backspaces](https://stackoverflow.com/q/16604523/215380). – Rawling Aug 14 '18 at 09:58
  • @Rawling Thanks, looking into that now. – Lauran Aug 14 '18 at 10:10