How can I split a string depending on its content?

user1012732

I am trying to parse a string and split it by some delimiters, also including the delimiters.

For example, from the string if(a>b) write(a); I want to get if,(,a,>,b,),write,(,a,),;

Here is what I've tried:

string pattern = "(" + String.Join("|", delimiters.Select(d =>Regex.Escape(d)).ToList()) + ")";
List<string> result = Regex.Split(line, pattern).ToList();

It works, but it fails in some cases. If I had the string if(a>0) write("it is positive"); I would not like to get "it,is,positive" (because space is a delimiter), but "it is positive". How can I do this?

Wiktor Stribiżew

Matching C strings can be achieved with a known regex:

"[^"\\]*(?:\\.[^"\\]*)*"

See regex demo

To incorporate it into your code, you just need to add the regex to the list of delimiters, but you need to place it as the first alternative in the capturing group.

var delimiters = new List<string> { " ", "(", ")", ">", "<", ",", ";"};
var line = "if(a>b) write(\"My new result\")";
var escaped_delimiters = new List<string>();
escaped_delimiters.Add(@"""[^""\\]*(?:\\.[^""\\]*)*""");
escaped_delimiters.AddRange(delimiters.Select(d => Regex.Escape(d)).ToList());
var pattern = "(" + String.Join("|", escaped_delimiters) + ")";
var result = Regex.Split(line, pattern).Where(x => !String.IsNullOrWhiteSpace(x)).ToList();

See IDEONE demo

If you need no empty elements, use

List<string> result = Regex.Split(line, pattern).Where(x => !string.IsNullOrWhiteSpace(x)).ToList();

The result will be

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I split a string into chunks?

From Dev

Scala: How can I split a String into a Map

From Dev

How can I parse a json and extract to different case classes depending of its content

From Dev

How can I split a string by newline in Shopify?

From Dev

Split vector into several vectors depending on its content

From Dev

How can I split a word into its component letters?

From Dev

javafx: How can I make a label automatically updates its text color depending on a String Property?

From Dev

How can I split this string in Python?

From Dev

How can I split string that have "[*word*]" as its part

From Dev

How can I split by the last point of a String?

From Dev

How can i split string with dot in makefile

From Dev

How can I split a string into an alist() in R?

From Dev

How can we adjust the height of a parent element depending on its content?

From Dev

How can I split a string into a struct?

From Dev

How can i split this string in Java?

From Dev

How can i split string with the operators?

From Dev

How can I encode string in file content?

From Dev

How can I split certain phrase a String?

From Dev

How I can split String on (:) but not time

From Dev

How can I split a String of dots?

From Dev

How can I split a string in LESS?

From Dev

How can I split an animated .gif file into its component frames?

From Dev

Split a String depending on its length

From Dev

How can I split a string with accents in PHP?

From Dev

How can I dynamically select and expand a certain search node depending on its score and the score of its children?

From Dev

How can I use regex to split a string by {}

From Dev

How can I split this string of numbers with ranges?

From Dev

How can I set split string in array?

From Dev

How can I split a string by words?

Related Related

  1. 1

    How can I split a string into chunks?

  2. 2

    Scala: How can I split a String into a Map

  3. 3

    How can I parse a json and extract to different case classes depending of its content

  4. 4

    How can I split a string by newline in Shopify?

  5. 5

    Split vector into several vectors depending on its content

  6. 6

    How can I split a word into its component letters?

  7. 7

    javafx: How can I make a label automatically updates its text color depending on a String Property?

  8. 8

    How can I split this string in Python?

  9. 9

    How can I split string that have "[*word*]" as its part

  10. 10

    How can I split by the last point of a String?

  11. 11

    How can i split string with dot in makefile

  12. 12

    How can I split a string into an alist() in R?

  13. 13

    How can we adjust the height of a parent element depending on its content?

  14. 14

    How can I split a string into a struct?

  15. 15

    How can i split this string in Java?

  16. 16

    How can i split string with the operators?

  17. 17

    How can I encode string in file content?

  18. 18

    How can I split certain phrase a String?

  19. 19

    How I can split String on (:) but not time

  20. 20

    How can I split a String of dots?

  21. 21

    How can I split a string in LESS?

  22. 22

    How can I split an animated .gif file into its component frames?

  23. 23

    Split a String depending on its length

  24. 24

    How can I split a string with accents in PHP?

  25. 25

    How can I dynamically select and expand a certain search node depending on its score and the score of its children?

  26. 26

    How can I use regex to split a string by {}

  27. 27

    How can I split this string of numbers with ranges?

  28. 28

    How can I set split string in array?

  29. 29

    How can I split a string by words?

HotTag

Archive