0

I would like to transform this claim that I get "[\" 75 \ ", \" 91 \ "]" to (75,91)

I used regex.split but I don't know how to do it.

if (Zone != null)
{
    filtrer.Append(" and depalcement in (");

    foreach (string i in Zone)
    {
        var diviser=Regex.Split(i, @"\");
        filtrer.Append(diviser);
    }
}
5
  • 7
    Is your input actually JSON? Then you´re better off using JsonConvert than trying to parse this yourself. Commented May 10, 2021 at 10:37
  • 1
    If your input is not JSON, show the actual input. Commented May 10, 2021 at 10:48
  • 1
    You are not trying to build up an SQL statement here, are you? Commented May 10, 2021 at 10:57
  • oui je veux de créer une Requete sql Commented May 10, 2021 at 11:39
  • 1
    Stack Overflow is english-only. Please try and stick to it, thanks. @KOMARA ( SO est en anglais uniquement. Si vous le pouvez, veuillez vous en tenir à l'anglais. ) Commented May 10, 2021 at 11:53

2 Answers 2

2

No Regex, just JSON. Try this:

var content = "[\"75\",\"91\"]";
var list = System.Text.Json.JsonSerializer.Deserialize<List<string>>(content);
    
var numbers = list.Select(int.Parse);
    
foreach(var number in numbers)
{
    Console.WriteLine(number);
}
Sign up to request clarification or add additional context in comments.

Comments

1

For completeness; to get from "[\" 75 \", \" 91 \"]" to (75,91) using regex the code could be something like this:

var s2 = "(" + Regex.Replace(input: s, pattern: "[^0-9,]", "") + ")"; // or @"[^\d,]"

Pattern:

  • [...] - character set aka character class aka 'any of'
  • ^ - negate
  • 0-9 or \d - digit
  • , comma
  • together it's any character that isn't a digit or a comma

Comments

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.