preg_split to seperate input [duplicate] - php

This question already has answers here:
Split string on spaces except words in quotes
(4 answers)
Closed 3 years ago.
I'm building a website using PHP.
I am using a preg_split() to separate a given string which looks like +word1 -word2 -"word word".
But I need them in the following form +word1, -word2, -"word word".
Currently, I have this one:
$words = preg_split("/[\s\"]*\"([^\"]+)\"[\s\"]*|" . "[\s\"]*'([^']+)'[\s\"]*|" . "[\s\"]+/", $search_expression, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
but it didn't work as I wish: I need to do it in this way to get it work:
+word1 -word2 '-"word word"'.
Does someone have a better regex or idea?

One option is to match from a double quote till a double quote and don't split on that match using SKIP FAIL. Then match 1+ horizontal whitespace chars to split on.
"[^"]+"(*SKIP)(*FAIL)|\h+
Regex demo | Php demo
For example
$search_expression = '+word1 -word2 -"word word"';
$words = preg_split("~\"[^\"]+\"(*SKIP)(*FAIL)|\h+~", $search_expression);
print_r($words);
Output
Array
(
[0] => +word1
[1] => -word2
[2] => -"word word"
)

A simpler expression with greedy ? works for matching your examples:
preg_match_all('/[+-][^+-]+ ?/', $search_expression, $matches);
print_r($matches[0]);
Yields:
Array
(
[0] => +word1
[1] => -word2
[2] => -"word word"
)
Se Example.

Related

Regex formula spliting with preg_split

Hello i am passing a bad moment trying to found the correct Regex formula.
$stringSplit = "+foo a -ba24+Sample3";
$vectorsPlus = preg_split('/[+*]/',$stringSplit ,-1,PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
$vectorsMinus = preg_split('/[-*]/',$stringSplit ,-1,PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
My objetive it's get 2 diferents arrays grouping on symbol like:
$vectorsPlus [0] = '+foo';
$vectorsPlus [1] = '+Sample3';
$vectorsMinus [0] = '-ba24';
I will aprecciate any help how to solves this with the Regex magic.
Instead of using split, you can match the values.
Note that [+*] and [-*] both match a * char and which is not in the example data.
You can match either a + or a - followed by matching the opposite excluding whitespace chars using a negated character class.
$stringSplit = "+foo a -ba24+Sample3";
preg_match_all("/\+[^-+\s]+/", $stringSplit, $matchesPlus);
print_r($matchesPlus[0]);
preg_match_all("/-[^-+\s]+/", $stringSplit, $matchesMinus);
print_r($matchesMinus[0]);
Output
Array
(
[0] => +foo
[1] => +Sample3
)
Array
(
[0] => -ba24
)
See a php demo

PHP split string into integer, string and special character

I need to split this format of strings CF12:10 into array like below,
[0] => CF, [1] => 12, [2] => 10
Numbers and String of the provided string can be any length. I have found the php preg_match function but don't know how to make regular expression for my case. Any solution would be highly appreciated.
You could use this regex to match the individual parts:
^(\D+)(\d+):(.*)$
It matches start of string, some number of non-digit characters (\D+), followed by some number of digits (\d+), a colon and some number of characters after the : and before end-of-line. In PHP you can use preg_match to then find all the matching groups:
$input = 'CF12:10';
preg_match('/^(\D+)(\d+):(.*)$/', $input, $matches);
array_shift($matches);
print_r($matches);
Output:
Array
(
[0] => CF
[1] => 12
[2] => 10
)
Demo on 3v4l.org
Try the following code if it helps you
$str = 'C12:10';
$arr = preg_match('~^(.*?)(\d+):(.*)~m', $str, $matches);
array_shift($matches);
echo '<pre>';print_r($matches);

PHP preg_split between Character (prefix) and Number [duplicate]

This question already has answers here:
preg_split() String into Text and Numbers [duplicate]
(2 answers)
Closed 4 years ago.
Sorry for my bad English, I would to ask about How to split characters and numbers using preg_split(). For example, I have any data with prefix like :
ABC00001 to array(0 => 'ABC', 1 => 00001)
DEFG00002 to array(0 => 'DEFG', 1 => 00002)
AB00003 to array(0 => 'AB', 1 => 00003)
Thanks for advise
Split on the zero-length position that follows the sequence of letters.
Code: Demo
$string = 'ABC00001';
$output = preg_split('~[A-Z]+\K~', $string);
var_export($output);
The \K says forget the previous matched characters.
You can use preg_split with the PREG_SPLIT_DELIM_CAPTURE and PREG_SPLIT_NO_EMPTY flags to do what you want:
$string = 'ABC00001';
$output = preg_split('/([A-Z]+)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
print_r($output);
Output:
Array ( [0] => ABC [1] => 00001 )
Demo on 3v4l.org

Geting two matches in a string using a regular expression [duplicate]

This question already has answers here:
PHP Regular Expression: string from inside brackets
(3 answers)
Closed 5 years ago.
My string:
fields[name_1]
I want to get fields and name_1 using regex.
I'm know about preg_match_all(), but I'm not friends with regular expressions.
This can be used for direct match:
$string = 'fields[name_1]';
preg_match('/(.+)\[(.+)\]/', $string, $matches);
print_r($matches);
You get:
Array
(
[0] => fields[name_1]
[1] => fields
[2] => name_1
)
So, $matches[1] and $matches[2] are what you needed.
Still I am unclear about your exact need!
Here are the explanation for the Regex:
https://regex101.com/r/PcJzQL/3
http://www.phpliveregex.com/
https://www.functions-online.com/preg_match.html
THere are uncounted examples for this alone here on SO. A simple search would have shown you what you need. Anyway, to get you going:
<?php
$subject = 'fields[name_1]';
preg_match('/^(.+)\[(.+)]$/', $subject, $tokens);
print_r($tokens);
The output of that obviously is:
Array
(
[0] => fields[name_1]
[1] => fields
[2] => name_1
)

Using regex to not match periods between numbers

I have a regex code that splits strings between [.!?], and it works, but I'm trying to add something else to the regex code. I'm trying to make it so that it doesn't match [.] that's between numbers. Is that possible? So, like the example below:
$input = "one.two!three?4.000.";
$inputX = preg_split("~(?>[.!?]+)\K(?!$)~", $input);
print_r($inputX);
Result:
Array ( [0] => one. [1] => two! [2] => three? [3] => 4. [4] => 000. )
Need Result:
Array ( [0] => one. [1] => two! [2] => three? [3] => 4.000. )
You should be able to split on this:
(?<=(?<!\d(?=[.!?]+\d))[.!?])(?![.!?]|$)
https://regex101.com/r/kQ6zO4/1
It uses lookarounds to determine where to split. It looks behind to try to match anything in the set [.!?] one or more times as long as it isn't preceded by and succeeded by a digit.
It also won't return the last empty match by ensuring the last set isn't the end of the string.
UPDATE:
This should be much more efficient actually:
(?!\d+\.\d+).+?[.!?]+\K(?!$)
https://regex101.com/r/eN7rS8/1
Here is another possibility using regex flags:
$input = "one.two!three???4.000.";
$inputX = preg_split("~(\d+\.\d+[.!?]+|.*?[.!?]+)~", $input, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
print_r($inputX);
It includes the delimiter in the split and ignores empty matches. The regex can be simplified to ((?:\d+\.\d+|.*?)[.!?]+), but I think what is in the code sample above is more efficient.

Categories