Jump to content

Match first & last letters A or Z Only


Go to solution Solved by kicken,

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

  • Solution

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 ($)'

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.