Php - separation by unknown regular expression

I need to break the string with the known seperators, as well as the unknown ones. For example, I know that I want to split the string into "\ n" and "," and ".". but also 1 sphere that can be defined by the user: for example, it can be ";" or hello or just about anything.

I tried this:

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

... but this does not work properly. As I understand it | means or. Thus, this exp register should say that it is split into "\ n" or "," or ".". or hello. I think, because if I try only [hello], then it is split into every letter, not the whole word. This is strange because if I try just [\ n], then it only splits into "\ n" - not to "\" or "n".

Can someone explain this to me? :)

+4
source share
6 answers

When you put a bunch of characters in a character class, as in [hello] , this defines a token that matches a single character, which is either h, e, l, or o. In addition, | doesn't make sense inside a character class - it just matches a normal character.

The correct solution is not to use a character class - you should use regular brackets:

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

By the way, make sure you avoid any regular expression metacharacters found in $exp . In principle, the complete list here should be escaped using backslashes: http://regular-expressions.info/reference.html There may be an auxiliary function for this.

EDIT: since you are not using a character class, now we need to escape from \ . , which is now a metacharacteristic meaning of "match one out of nothing." Almost forgot.

+6
source

\n is actually only one character, a new line ( \ before n indicates the escape sequence), so why does it work and hello does not work.

Also, keep in mind that allowing arbitrary input into a regular expression can be a security risk, depending on what your regular expression is used for, so be very careful and make sure you sanitize your entry into this regular expression.

+1
source

Try using this regex:

 preg_split('#[\n,.]|'.$exp.'#', ...); 

Pay attention to single quotas to avoid replacing \n with a new line.

+1
source

Drop [ and ] as they define a character class. \n counted as one character in a line with two quotation marks. Just using a string without a character class should work as you need:

 preg_split("/\n|,|.|$exp/", $input) 
+1
source

Use preg_split ()

For instance:

Input:

 $exp = '#'; preg_split("/[,.\n$exp]/", "0\n1,2.3#4") 

Output:

 Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4) 
+1
source

here's a simple solution:

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

or you can do it like:

 "([\n,.]|".$exp.")" 
+1
source

Source: https://habr.com/ru/post/1481573/


All Articles