TtT23
TtT23

Reputation: 7030

C# Regex for retrieving capital string in quotation mark

Given a string, I want to retrieve a string that is in between the quotation marks, and that is fully capitalized.

For example, if a string of

oqr"awr"q q"ASRQ" asd "qIKQWIR"

has been entered, the regex would only evaluate "ASRQ" as matching string.

What is the best way to approach this?

Edit: Forgot to mention the string takes a numeric input as well I.E: "IO8917AS" is a valid input

Upvotes: 0

Views: 431

Answers (3)

Jon Skeet
Jon Skeet

Reputation: 1500575

EDIT: If you actually want "one or more characters, and none of the characters is a lower-case letter" then you probably want:

Regex regex = new Regex("\"\\P{Ll}+\"");

That will then allow digits as well... and punctuation. If you want to allow digits and upper case letters but nothing else, you can use:

Regex regex = new Regex("\"[\\p{Lu}\\d]+\"");

Or in verbatim string literal form (makes the quotes more confusing, but the backslashes less so):

Regex regex = new Regex(@"""[\p{Lu}\d]+""");

Original answer (before digits were required)

Sounds like you just want (within the pattern)

 "[A-Z]*"

So something like:

Regex regex = new Regex("\"[A-Z]*\"");

Or for full Unicode support, use the Lu Unicode character category:

Regex regex = new Regex("\"\\p{Lu}*\"");

EDIT: As noted, if you don't want to match an empty string in quotes (which is still "a string where everything is upper case") then use + instead of *, e.g.

Regex regex = new Regex("\"\\p{Lu}+\");

Short but complete example of finding and displaying the first match:

using System;
using System.Text.RegularExpressions;

class Program
{    
    public static void Main()
    {
        Regex regex = new Regex("\"\\p{Lu}+\"");
        string text = "oqr\"awr\"q q\"ASRQ\" asd \"qIKQWIR\"";

        Match match = regex.Match(text);
        Console.WriteLine(match.Success); // True
        Console.WriteLine(match.Value);   // "ASRQ"
    }    
}

Upvotes: 3

Ian Newson
Ian Newson

Reputation: 7949

Please try the following:

[\w]*"([A-Z0-9]+)"

Upvotes: 0

Jirka Hanika
Jirka Hanika

Reputation: 13529

Like this: "\"[A-Z]+\""

The outermost quotes are not part of the regex, they delimit a C# string.

This requires at least one uppercase character between quotes and works for the English language.

Upvotes: 0

Related Questions