B
B
BarneyGumble2018-02-14 19:55:35
PHP
BarneyGumble, 2018-02-14 19:55:35

What's wrong with regex?

Good evening. I'm trying to split a string with a regular expression. Everything is fine here:

$keywords = preg_split("/[\s,]+/", "hypertext language, programming");
print_r($keywords); // Array([0] => hypertext [1] => language [2] => programming)

If I change the condition, and I want to split into two parts, separated by the first closing bracket, then I get nothing on the left:
$keywords = preg_split("/^[^)]+/", "hypertext) language), programming");
print_r($keywords); // Array ( [0] => [1] => ) language), programming )
//хотя я ожидаю увидеть Array ( [0] => hypertext [1] => ) language), programming )

I checked my regular expression /^[^)]+/ on https://regex101.com/ - there it correctly finds the occurrence up to the first closing bracket
. What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nick Sdk, 2018-02-14
@BarneyGumble

you won't succeed
because you start eating
you should understand what preg_split
does - it splits a string according to a regular expression.
if we have a regular expression ^[^)]+"bites" this part:
then you will get the result:
Array ( [0] => [1] => ) language), programming )

S
synapse_people, 2018-02-14
@synapse_people

escape ) - \)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question