23

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 removing 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.

6
  • What language/framework are you using? Surely there's something built-in that will allow you to test a string's length without resorting to a regex? Commented Jan 19, 2011 at 12:25
  • @Luke: it doesn't seem to be just string length issue. Commented Jan 19, 2011 at 12:26
  • Why {20,40} if you want strings of length <= 22? Commented Jan 19, 2011 at 12:26
  • @SilentGhost: The question says "make sure that none of the names are longer than 22 characters" which sounds like a string length issue to me, although the question is a bit vague. David should clarify the requirements. Commented Jan 19, 2011 at 12:29
  • @Luke: yes and regex shows that OP's checking for specific characters. Commented Jan 19, 2011 at 12:30

4 Answers 4

56

This will match "At least 22 any characters"

.{22,} 
Sign up to request clarification or add additional context in comments.

4 Comments

So as soon as the file gets 22 character long or longer, then this will match... the whole file.
Why? did you try this? It should match line-by-line
+1 Contrary to Lazarus' solution, this works just fine.
This would be the best answer
29

The regex would be:

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

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

1 Comment

Bingo! Thanks, I shall save this somewhere special for future use :)
4

This regex will match a string longer than 22 chars

/.{23,}/ 

1 Comment

So as soon as the file gets 22 character long or longer, then this will match... the whole file.
2

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

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

2 Comments

I'm not processing the file, it's flat. Therefore it's not possible to parse the array using PHP. I'm looking for a regex solution to allow me to apply a similar technique in the future :)
regex is better way instead of fucntion

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.