How can I split a string on separators, and keep the separators as elements in the resulting array?

Aviv Cohn

This question has been asked on this site in two ways: first way was actually a different question; the OP wanted to keep the separators at the end/beginning of the resulting elements. The second question received an answer that included a Regex that I couldn't understand how to expand.

I'm looking for a way to split a string on separators, where the separators will be included as elements in the resulting array. Please explain how I can choose custom separators for this function (most possibly a Regex).

Sergey Kalinichenko

You can use a regex that specifies an empty expression with a lookahead or a lookbehind.

For example, let's say that you wish to split your string on any of these characters:

'(' ' ' ',' ')' '*' '/' '+' '-'

Then you can construct an expression that lists them in a lookahead or a lookbehind clause (i.e. (?=...) or (?<=...)), and split using Regex.Split, like this:

string input = "12 / 34+(45-56)*678";
string pattern = "(?=[( ,)*/+-])|(?<=[( ,)*/+-])";

string[] substrings = Regex.Split(input, pattern);
foreach (string match in substrings) {
    Console.WriteLine("'{0}'", match);
}

Demo.

Running this produces the following output:

'12'
' '
'/'
' '
'34'
'+'
'('
'45'
'-'
'56'
')'
'*'
'678'

Note how all separators are included, along with spaces, parentheses, and operators.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

PHP logical key/value array separators

분류에서Dev

Extracts all sub strings between string separators in a string (C#)

분류에서Dev

What are the readline word separators?

분류에서Dev

how can i use split() with a big number of elements, java

분류에서Dev

Print matched field separators in awk

분류에서Dev

Print matched field separators in awk

분류에서Dev

import complex data structure in hive with custom separators

분류에서Dev

How can i make array tree from string?

분류에서Dev

How do I Split a string by empty lines?

분류에서Dev

How do I setup a project with autotools so resulting executables can see required binary files?

분류에서Dev

Using a Javascript switch statement, how can I count the number of elements (numbers only) in an array?

분류에서Dev

change all new line separators to crlf from cr or crlf on bytes

분류에서Dev

Angular split string to array

분류에서Dev

How can I organize this array

분류에서Dev

How to find duplicate elements in a string array and store the result in another array

분류에서Dev

How can I keep object from passing to another form?

분류에서Dev

java Android - how can I keep Image's actual size?

분류에서Dev

How can I keep the bubble from a JRadioButton when adding an ImageIcon?

분류에서Dev

When using CI, how can I keep my environments clean?

분류에서Dev

How can I work on files on my server and keep them in sync?

분류에서Dev

How can I work on files on my server and keep them in sync?

분류에서Dev

How do I add the elements of an array to another array

분류에서Dev

How can I realize that android send one string to a php page and obtain a JSON array?

분류에서Dev

How do I split an array into two different arrays?

분류에서Dev

How can I create a table of only the unique elements in a list so I can order the elements in terms of frequency?

분류에서Dev

How can I split two datetime into one range?

분류에서Dev

How can I split a PDF's pages down the middle?

분류에서Dev

How can I split a .mp4 in two?

분류에서Dev

Can I keep . and .. out of .* expansion?

Related 관련 기사

  1. 1

    PHP logical key/value array separators

  2. 2

    Extracts all sub strings between string separators in a string (C#)

  3. 3

    What are the readline word separators?

  4. 4

    how can i use split() with a big number of elements, java

  5. 5

    Print matched field separators in awk

  6. 6

    Print matched field separators in awk

  7. 7

    import complex data structure in hive with custom separators

  8. 8

    How can i make array tree from string?

  9. 9

    How do I Split a string by empty lines?

  10. 10

    How do I setup a project with autotools so resulting executables can see required binary files?

  11. 11

    Using a Javascript switch statement, how can I count the number of elements (numbers only) in an array?

  12. 12

    change all new line separators to crlf from cr or crlf on bytes

  13. 13

    Angular split string to array

  14. 14

    How can I organize this array

  15. 15

    How to find duplicate elements in a string array and store the result in another array

  16. 16

    How can I keep object from passing to another form?

  17. 17

    java Android - how can I keep Image's actual size?

  18. 18

    How can I keep the bubble from a JRadioButton when adding an ImageIcon?

  19. 19

    When using CI, how can I keep my environments clean?

  20. 20

    How can I work on files on my server and keep them in sync?

  21. 21

    How can I work on files on my server and keep them in sync?

  22. 22

    How do I add the elements of an array to another array

  23. 23

    How can I realize that android send one string to a php page and obtain a JSON array?

  24. 24

    How do I split an array into two different arrays?

  25. 25

    How can I create a table of only the unique elements in a list so I can order the elements in terms of frequency?

  26. 26

    How can I split two datetime into one range?

  27. 27

    How can I split a PDF's pages down the middle?

  28. 28

    How can I split a .mp4 in two?

  29. 29

    Can I keep . and .. out of .* expansion?

뜨겁다태그

보관