0

Regex newbie here, so I was trying this website for fun: https://regex.alf.nu

In particular, I'm concerned about the "Ranges" section here: https://regex.alf.nu/2

I was able to get as far as ^[a-f]+, and couldn't figure out the rest. By accident, I added a $ to get ^[a-f]+$ which was actually the answer.

Trying to wrap my mind around the meaning of this regex. Can someone give the plain English explanation of what's happening here?

It seems to say "a string that starts and ends with one or more of the letters a through f," but that doesn't quite make sense for me, for instance, with the word "cajac" which seems to satisfy those conditions.

For those who can't see the URL, it's asking me to match these words:

abac
accede
adead
babe
bead
bebed
bedad
bedded
bedead
bedeaf
caba
caffa
dace
dade
daff
dead
deed
deface
faded
faff
feed

But NOT match these:

beam
buoy
canjac
chymia
corah
cupula
griece
hafter
idic
lucy
martyr
matron
messrs
mucose
relose
sonly
tegua
threap
towned
widish
yite
skippr
  • 2,326
  • 3
  • 21
  • 36
  • @iamnotmaynard This is not a "give me ze code" question, nor is it lacking effort on my part (I've provided my own answer)...I've just been unable to understand it fully, and am asking for a better explanation. – skippr Sep 24 '15 at 17:18
  • @anubhava cajac does not match, and I'm aware of this. I'm not concerned with the what here, but the why. – skippr Sep 24 '15 at 17:22

2 Answers2

1

In English it means: Match any words which contain only the letters a thru f.

skippr
  • 2,326
  • 3
  • 21
  • 36
hata
  • 8,429
  • 5
  • 32
  • 57
  • 2
    Ah, ok, so this makes sense. I misunderstood the starts and ends with operators then. The way I saw it, it was "the string must start with one more letters (a-f), and must end with one more letters (a-f)....which in my mind seemed to apply to "cajac" which starts with "ca" and ends with "ac" – skippr Sep 24 '15 at 17:20
0

Your pattern, when broken down:

  • ^ assert position at start of the string
  • [a-f]+ match a single character present in the list below:

    • + Between one and unlimited times, as many times as possible, giving back as needed
    • a-f a single character in the range between a and f (case sensitive)
  • $ assert position at end of the string

You can also see a quick explanation of your patterns on the Regex101 webpage.

hjpotter92
  • 71,576
  • 32
  • 131
  • 164
  • This was how I understood it, but I seem to be interpreting it differently.The way I saw it, it was "the string must start with one or more letters (a-f), and must end with one or more letters (a-f)....which in my mind seemed to apply to "cajac" which starts with "ca" and ends with "ac" – skippr Sep 24 '15 at 17:21
  • @dhira Since you are anchoring your pattern (using `^` and `$`), it will match only those string which satisfy the conditions as enclosed by the anchors. – hjpotter92 Sep 24 '15 at 17:26
  • https://regex101.com/r/vI4bX8/1 – Reinstate Monica -- notmaynard Sep 24 '15 at 17:38