Jump to content

Match first & last letters A or Z Only


n1concepts

Recommended Posts

I need to match only the files whose names start with a or z and end with a or z?

I'm struggling with the reg to do this.... This what i have thus far but doesn't block to just the 1st letter 'a' or 'z' at beginning of a word or the last letter (same) 'a' or 'z'.

 

Example:

 

Regular Exp in 'ls' statement:     ls [az]*[az$]

 

Results: afkfz akfkea az za zdea zdkdkz

However, the problem is [az$] also allowing:  'a', a 'z', or a '$'.

 

Question: what am I doing wrong w/this regular expression?    [az]*[az$]

Should the $ sign be outside the bracket? That didn't work for me - any suggestions appreciated.

 

Thx!

Link to comment
https://forums.phpfreaks.com/topic/284372-match-first-last-letters-a-or-z-only/
Share on other sites

Question: what am I doing wrong w/this regular expression?    [az]*[az$]

That regex says 'A or Z, repeated any number of times, followed by A, Z or $'

 

The regex you are looking for would be: ^[az].*[az]$

That regex says 'At the start of the string (^) look for either a or z ([az]), then any character (.) any number of times (*) followed by either a or z ([az]) then the end of the string ($)'

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.