开发者

Trying to regex for strings longer than 22 characters

开发者 https://www.devze.com 2023-02-05 17:45 出处:网络
I have a very long array of data, and I need to quickly whizz through it and make sure that none of the names are longer than 22 characters. I understand that I could truncate it on the display side,

I have a very long array of data, and I need to quickly whizz through it and make sure that none of the names are longer than 22 characters. I understand that I could truncate it on the display side, but I would rather tackle this with a proper solution, of just remov开发者_如何转开发ing them :)

This is my sample

$profiles[257] = array('name'=>'FedupKissingFrogs', 'age'=>27, 'sex'=>'F', 'location'=>'XXXXXXXXXX');
$profiles[260] = array('name'=>'Lil_Greta_90', 'age'=>20, 'sex'=>'F', 'location'=>'XXXXXXXXXX');
$profiles[262] = array('name'=>'lOOkfOrme86', 'age'=>24, 'sex'=>'F', 'location'=>'XXXXXXXXXX');
$profiles[259] = array('name'=>'youvefoundME', 'age'=>21, 'sex'=>'F', 'location'=>'XXXXXXXXXX');

And here is the regex that I have come up with so far, which doesn't seem to work at all

'[A-Za-z]{20,40}'

My plan is that I can use the regex to mark the lines and then I can delete them from within my IDE. There is no programming allowed ;)

-- Edit --

Thanks for all the replies! The idea behind this was a quick and automated way to just scan a flat PHP file containing an array to see if all the names where shorter than 22 characters, as a name longer than that will break the layout, and I've been asked to remove them. I wanted to just search in my IDE and remove the lines.

Matching the characters isn't important as such, any characters are allowable, even space, \ / ~ and * etc. I'm looking more to match length of the string but contained in the =>'$name' container.


This will match "At least 22 any characters"

.{22,}


The regex would be:

/'name'=>'[^']{23,}?'/i

This will match any line with a 'name' that is 23 characters or longer.


This regex will match a string longer than 22 chars

/.{23,}/


I couldn't resist using the good ol' strlen.

foreach ($profiles as $id => $data) {
    if (strlen($data['name']) >= 22)
        unset($profiles[$id]);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消