-1

How would I check a string contains at least one lowercase and at least one uppercase using awk.

My attempt:

^.*[a-z]+[A-Z]+.*$|^.*[A-Z]+[a-z]+.*$
War10ck
  • 12,387
  • 7
  • 41
  • 54
  • 4
    Show your attempt please. – Jerry Jun 03 '13 at 16:18
  • 1
    Try this link: http://stackoverflow.com/questions/1559751/regex-to-make-sure-that-the-string-contains-at-least-one-lower-case-char-upper – farmbytes Jun 03 '13 at 16:23
  • I wrote a pretty in depth "Password matching" drop in regular expression for this question: http://stackoverflow.com/questions/16717656/regex-no-more-than-2-identical-consecutive-characters-and-a-z-and-0-9/16717823#16717823 Maybe it will help. – FrankieTheKneeMan Jun 03 '13 at 16:23
  • my attempt: ^.*[a-z]+[A-Z]+.*$|^.*[A-Z]+[a-z]+.*$ – user2448619 Jun 03 '13 at 16:24
  • 4
    Is there a reason that none of the other 20 questions here asking the same thing wouldn't work for you, like http://stackoverflow.com/q/1154985/62576 or http://stackoverflow.com/q/16689167/62576? – Ken White Jun 03 '13 at 16:24

2 Answers2

2

With awk you can use the logical operator && and test for both lowercase and uppercase using their respective character classes:

$ cat file
abc
ABC
aBc
123

$ awk '/[a-z]/&&/[A-Z]/{print $0,"[PASS]";next}{print $0,"[FAIL]"}' file
abc [FAIL]
ABC [FAIL]
aBc [PASS]
123 [FAIL]
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
0

Try this. ;)

.*(?=.*[a-z])(?=.*[A-Z]).*

. = multiple times

* = any char

?= = last check should be true

[a-z]/[A-Z] = should contain the range of a-z and A-Z

You can test anytime your regex here: Regex Tester

Denny Crane
  • 637
  • 6
  • 19
  • You don't need the opening or closing `.*` for this regex. If you're using any anchoring methodology (like Javascript's .match), then at most you need one. (I'd select the closing one). – FrankieTheKneeMan Jun 03 '13 at 16:22
  • awk supports regex. But note that the regex implementations can be different from each tool and OS. You can tell awk which regex you prefer. I use mostly the perl style regex. – Denny Crane Jun 03 '13 at 16:28
  • 1
    No implementations of `awk` support look-arounds. – Chris Seymour Jun 03 '13 at 19:48