Amran
Amran

Reputation: 657

Extract substrings before or after underscore

I tried to use the preg_match_all() function to search for a string after the _. The output that I want will be reset,text,email. I tried to make it using the regexr editor and able to make it with [_]+[a-z]* but this will include the _reset, _text, _text. The string will be:

$str = 'button_reset,location_text,email_text';

Expected output:

reset
text
email

Upvotes: 0

Views: 479

Answers (2)

mickmackusa
mickmackusa

Reputation: 47854

It will be better to avoid regex for this task and just use str_replace():

Input:

$str = 'button_reset,location_text,email_text';

Code for output as array:

var_export(explode(',',str_replace(['button_reset','location_text','email_text'],['reset','text','email'],$str)));
// array (
//    0 => 'reset',
//    1 => 'text',
//    2 => 'email',
// )

Or if you insist, Regex (Demo Link):

/button_\K[^,]+|,location_\K[^,]+|,\K[^_]+(?=_text)/

Regex Breakdown:

button_\K[^,]+     #Match one or more non-comma-characters after button_
|                  #or
,location_\K[^,]+  #Match one or more non-comma-characters after location_
|                  #or
,\K[^_]+(?=_text)  #Match one or more non-underscore-characters that are
                   # immediately followed by _textafter button_

The \K in each conditional expression means match from this point and effectively removes the need to use capture groups for this case. When using capture groups, preg_match_all() creates multiple subarrays -- one filled with fullstring matches, and at least one more with captured values. \K should be used whenever possible because it cuts array size by up to 50%.

Code:

$array=preg_match_all('/button_\K[^,]+|,location_\K[^,]+|,\K[^_]+(?=_text)/',$str,$out)?$out[0]:[];
var_export($array);

Same output:

array ( 0 => 'reset', 1 => 'text', 2 => 'email', )

Upvotes: 2

Sahil Gulati
Sahil Gulati

Reputation: 15141

Regex: /\_\K[a-zA-Z0-9]+

1. \_\K This will match _ and \K will reset the whole match.

2. [a-zA-Z0-9]+ will match all these characters

Try this code snippet here

<?php

ini_set('display_errors', 1);
$str = 'button_reset,location_text,email_text';
preg_match_all("/\_\K[a-zA-Z0-9]+/",$str,$matches);
print_r($matches);

Output:

Array
(
    [0] => Array
        (
            [0] => reset
            [1] => text
            [2] => text
        )
)

Upvotes: 3

Related Questions