John Davidson

regex - PHP preg_split() if it is NOT a number, + sign, bracket, new line or tab

0 comments
Message:


I have the following operation which splits a string whenever it encounters an alphabet:


$splitOriginal = preg_split("/[a-zA-Z]/", implode('', array_slice($array, $key)), 2);

I want to extend it in such a way that it splits the string whenever the variable encountered is NOT a number, bracket, plus sign, hyphen, newline or tab.


I have written a regex which can match the above:


preg_match('/^(?=.*[0-9])[- +()0-9\r\n\t]+$/', $value)

But I need to negate it, to match whenever the value being compared is NOT it. Would really appreciate any prompt help I can get.


Thank you.



My Answer:
'/[a-zA-Z]/', $originalString);

To split the string whenever it encounters a character that is not a number, sign, bracket, new line, or tab, you can use the following regex pattern:

php
$splitOriginal = preg_split('/[^0-9\(\)\[\]\{\}\+\-\*\/\n\t]/', $originalString);


This pattern uses the `^` symbol inside the square brackets to match any character that is not a number (`0-9`), opening or closing brackets (`\(\)`, `\[ \]`, `\{ \}`), arithmetic operators (`\+\-\*\/`), new line (`\n`), or tab (`\t`).

Rate this post

3 of 5 based on 2569 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us