Asked  7 Months ago    Answers:  5   Viewed   35 times

I have some strings which can be in the following format

sometext moretext 01 text
text sometext moretext 002
text text 1 (somemoretext)
etc

I want to split these strings into following: text before the number and the number

For example: text text 1 (somemoretext)
When split will output:
text = text text
number = 1

Anything after the number can be discarded

Have read up about using regular expressions and maybe using preg_match or preg_split but am lost when it comes to the regular expression part

 Answers

75
preg_match('/[^d]+/', $string, $textMatch);
preg_match('/d+/', $string, $numMatch);

$text = $textMatch[0];
$num = $numMatch[0];

Alternatively, you can use preg_match_all with capture groups to do it all in one shot:

preg_match_all('/^([^d]+)(d+)/', $string, $match);

$text = $match[1][0];
$num = $match[2][0];
Wednesday, March 31, 2021
 
Nickool
answered 7 Months ago
50

You can tell preg_split() to split at any point in the string which is followed by three digits by using a lookahead assertion.

$str = "101WE3P-1An Electrically-Small104TU5A-3,Signal-Interference Duplexers Gomez-GarciaRobertoTU5A-3-01";
$result = preg_split('/(?=d{3})/', $str, -1, PREG_SPLIT_NO_EMPTY);

var_export($result);

Gives the following array:

array (
  0 => '101WE3P-1An Electrically-Small',
  1 => '104TU5A-3,Signal-Interference Duplexers Gomez-GarciaRobertoTU5A-3-01',
)

The PREG_SPLIT_NO_EMPTY flag is used because the very start of the string is also a point where there are three digits, so an empty split happens here. We could alter the regex to not split at the very start of the string but that would make it a little more difficult to understand at-a-glance, whereas the flag is very clear.

Saturday, May 29, 2021
 
turik
answered 5 Months ago
87

This regex will do the trick:

(d+)d (d+)h (d+)m (d+)s

Each value (day, hour, minute, second) will be captured in a group.

About your regex: I don't know what do you mean by "isn't correct", but I guess it's probably failing because your regex is greedy instead of lazy (more info). Try using lazy operators, or using more specific matches (d instead of ., for example).

EDIT:

I need them to be separate variables

After matching, they will be put in different locations in the resulting array. Just assign them to variables. Check out an example here.

If you have trouble understanding the resulting array structure, you may want to use the PREG_SET_ORDER flag when calling preg_match_all (more information here).

Saturday, May 29, 2021
 
relyt
answered 5 Months ago
15

I would approach this by using re.match in the following way:

import re
match = re.match(r"([a-z]+)([0-9]+)", 'foofo21', re.I)
if match:
    items = match.groups()
print(items)
>> ("foofo", "21")
Wednesday, June 9, 2021
 
Savageman
answered 5 Months ago
38

You can use preg_split

$string = "Hi, my name is Bob. I m 19yo and 170cm tall";
$parts = preg_split("/(,?s+)|((?<=[a-z])(?=d))|((?<=d)(?=[a-z]))/i", $string);
var_dump ($parts);

When matching against the digit-letter boundary, the regular expression match must be zero-width. The characters themselves must not be included in the match. For this the zero-width lookarounds are useful.

http://codepad.org/i4Y6r6VS

Saturday, August 14, 2021
 
waylaidwanderer
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share