-4

I'd like to use Regex to ensure that there are at least two upper case characters in a string, in any position, together or not.

The following gives me two together:

([A-Z]){2}

Environment - Classic ASP VB.

Leigh
  • 28,424
  • 10
  • 49
  • 96
Boomfelled
  • 463
  • 4
  • 14
  • 5
    The simple answer: `[A-Z].*[A-Z]`. [Illustrated here](https://regex101.com/r/KmQnrE/1) – SamWhan Apr 25 '17 at 20:03
  • That works, thanks for your help. Although this a comment, not an answer so I cannot mark it as a solution. Shall I mark the one below as the solution? Thanks again. – Boomfelled Apr 26 '17 at 09:45

3 Answers3

3

[A-Z].*[A-Z]

A to Z , any symbols between, again A to Z

update

As Wiktor mentioned in comments:

This regex will check for 2 letters on a line (in most regex flavors), not in a string.

So

[A-Z][^A-Z]*[A-Z]

Should do the thing (In most regex flavors/tools)

bumbeishvili
  • 1,197
  • 13
  • 24
1

You can use the simple regex

[A-Z].*[A-Z]

which matches an upper case letter, followed by any number of anything (except linefeed), and another uppercase letter.

If you need it to allow linefeeds between the letters, you'll have to set the single line flag. If you're using JavaScript (U should always include flavor/language-tag when asking regex-related questions), it doesn't have that possibility. Then the solution suggested by Wiktor S in a comment to another answer should work.

SamWhan
  • 8,038
  • 1
  • 14
  • 42
0

I believe what you're looking for is something like this:

.*([A-Z]).*([A-Z]).*

Broken out into segments thats:

.*        //Any number of characters (including zero)
([A-Z])   //A capital letter
.*        //Any number of characters (including zero)
([A-Z])   //A second capital letter
.*        //Any number of characters (including zero)
  • .* is redundant on sides – bumbeishvili Apr 25 '17 at 20:10
  • Wouldn't it just match the capitals and the characters between them without that (eg: http://www.regextester.com/?fam=97470)? If OP wants it to match the whole string, I'd generally include them, though I'm not familiar with ASP VB, so I could be wrong for this use case. – teaguehopkins Apr 25 '17 at 20:16