How do I split a whitespace delimited string of tokens with an escape character?

Nick Strupat

I need to tokenize a whitespace delimited string like hey 'this is' some text into an array ['hey', 'this is', 'some', 'text'] (single quote character being the escape character).

What I have so far will split on whitespace, but it doesn't incorporate the necessary escape character.

$tokens = preg_split('/[\ \n\,]+/', $whitespaceDelimitedString);

Regular expression ninjas, come forth!! Please and thanks.

anubhava

You can use this code:

$s = "hey 'this is' some text";
$a = preg_split("/'([^']*)'\s*|\s+/", $s, 0, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
print_r($a);

OUTPUT:

Array
(
    [0] => hey
    [1] => this is
    [2] => some
    [3] => text
)

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 do I read whitespace-delimited words from a stream?

From Java

How do I split a string, breaking at a particular character?

From Dev

How do I split a string by character position in c

From Dev

How do I split a string by character position in c

From Dev

How do I split a string at the third occurrence of the character? [android]

From Dev

I want to perform a split() on a string using a regex in Java, but would like to keep the delimited tokens in the array

From Dev

How do I split a linefeed delimited string in awk and use its output on the same command line in bash?

From Dev

How do I split a CR-delimited string into multiple lines and append id to each new line?

From Dev

How to Split String with delimited string "#|#"

From Dev

How do I escape a special Regex character?

From Dev

C#: How do i split a string on a whitespace after 100 characters have been reached

From Dev

How do I escape an escape character with ANTLR 4?

From Dev

python split string on whitespace following specific character

From Java

How do I trim whitespace from a string?

From Dev

How do I slice a string if whitespace is detected?

From Dev

How do I split this delimited text in to key and value using a Regex?

From Dev

.NET Regex: How do I include "end of string only" character escape in a bracket expression?

From Dev

How do I use string formatting with explicit character escape codes in Python?

From Dev

How can I split a String by all whitespace or commas

From Dev

How do I correctly escape the double quotation mark when using string.split

From Dev

How can I split a string into tokens in Javascript, including the white spaces?

From Dev

How can I split a string into tokens in C#?

From Dev

How do I escape a string for HTML?

From Dev

How do I concatenate an escape to a string?

From Dev

How do I escape quotes in a string variable?

From Dev

How do I escape a String for XML?

From Dev

How split java string using character which does not have an escape character before it

From Dev

How do I ignore whitespace between tokens in c++ regex (free spacing mode)

From Dev

How do I ignore whitespace between tokens in c++ regex (free spacing mode)

Related Related

  1. 1

    How do I read whitespace-delimited words from a stream?

  2. 2

    How do I split a string, breaking at a particular character?

  3. 3

    How do I split a string by character position in c

  4. 4

    How do I split a string by character position in c

  5. 5

    How do I split a string at the third occurrence of the character? [android]

  6. 6

    I want to perform a split() on a string using a regex in Java, but would like to keep the delimited tokens in the array

  7. 7

    How do I split a linefeed delimited string in awk and use its output on the same command line in bash?

  8. 8

    How do I split a CR-delimited string into multiple lines and append id to each new line?

  9. 9

    How to Split String with delimited string "#|#"

  10. 10

    How do I escape a special Regex character?

  11. 11

    C#: How do i split a string on a whitespace after 100 characters have been reached

  12. 12

    How do I escape an escape character with ANTLR 4?

  13. 13

    python split string on whitespace following specific character

  14. 14

    How do I trim whitespace from a string?

  15. 15

    How do I slice a string if whitespace is detected?

  16. 16

    How do I split this delimited text in to key and value using a Regex?

  17. 17

    .NET Regex: How do I include "end of string only" character escape in a bracket expression?

  18. 18

    How do I use string formatting with explicit character escape codes in Python?

  19. 19

    How can I split a String by all whitespace or commas

  20. 20

    How do I correctly escape the double quotation mark when using string.split

  21. 21

    How can I split a string into tokens in Javascript, including the white spaces?

  22. 22

    How can I split a string into tokens in C#?

  23. 23

    How do I escape a string for HTML?

  24. 24

    How do I concatenate an escape to a string?

  25. 25

    How do I escape quotes in a string variable?

  26. 26

    How do I escape a String for XML?

  27. 27

    How split java string using character which does not have an escape character before it

  28. 28

    How do I ignore whitespace between tokens in c++ regex (free spacing mode)

  29. 29

    How do I ignore whitespace between tokens in c++ regex (free spacing mode)

HotTag

Archive