Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php - split by unknown regular expression

Tags:

regex

php

I need to split a string by seperators that are known to me and also unknown. For example i know i want to split the string by "\n" and "," and "." but also 1 sperator that can be user defined: for example it can be ";" or "hello" or pretty much anything.

I tried this:

"[\n|,|.|".$exp."]"

...but that didnt work as expected. As i understand | means or. So this reg exp should say that split by "\n" or "," or "." or "hello". I think its because if i try just [hello] then it splits by every letter, not the whole word. Thats strange because if i try just [\n] then it only splits by "\n" - not by "\" or "n".

Can someone please explain this to me? :)

like image 948
user1985273 Avatar asked Dec 21 '22 05:12

user1985273


1 Answers

When you place a bunch of characters in a character class, as in [hello], this defines a token that matches one character that is either h, e, l or o. Also, | has no meaning inside of a character class - it's just matched as a normal character.

The correct solution isn't to use a character class - you meant to use normal brackets:

(\n|,|\.|".$exp.")

By the way - make sure that you escape any regex metacharacters that are in $exp. Basically, the full list here needs to be escaped with backslashes: http://regular-expressions.info/reference.html There may be a helper function to do it for you.

EDIT: Since you're not using a character class, we now need to escape \ the . which is now a metacharacter meaning 'match one of anything'. Almost forgot.

like image 66
Patashu Avatar answered Dec 30 '22 19:12

Patashu