jpm
jpm

Reputation: 1083

Checking all letters appear at least once in String

I'm using this regex to check that every letter appears at least once:

String regex = "(?i)(?=.*a)(?=.*b)(?=.*c)(?=.*d)(?=.*e)(?=.*f)(?=.*g)(?=.*h)(?=.*i)(?=.*j)(?=.*k)(?=.*l)(?=.*m)(?=.*n)(?=.*o)(?=.*p)(?=.*q)(?=.*r)(?=.*s)(?=.*t)(?=.*u)(?=.*v)(?=.*w)(?=.*x)(?=.*y)(?=.*z).*
"

Example:

The quick brown fox jumps over the lazy dog

contains all the english letters but my regex is not working.

What I'm doing wrong, or is there any other way to check for pangrams.

Upvotes: 2

Views: 317

Answers (2)

Kristján
Kristján

Reputation: 18803

There is a significantly simpler and faster way than regular expressions to check that a string is a pangram.

In small steps,

  • Convert the string to lowercase
  • Reduce it to unique letters
  • Sort it
  • Does it equal "abcdefghijklmnopqrstuvwxyz"?

Upvotes: 2

vks
vks

Reputation: 67968

    (?i)^(?=.*a)(?=.*b)(?=.*c)(?=.*d)(?=.*e)(?=.*f)(?=.*g)(?=.*h)(?=.*i)(?=.*j)(?=.*‌​k)(?=.*l)(?=.*m)(?=.*n)(?=.*o)(?=.*p)(?=.*q)(?=.*r)(?=.*s)(?=.*t)(?=.*u)(?=.*v)(?‌​=.*w)(?=.*x)(?=.*y)(?=.*z).*$

Add anchors ^$ to make a strict match instead of partial matches.

See demo.

https://regex101.com/r/vV1wW6/11

Upvotes: 1

Related Questions