Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression Pattern to Match Words in All Caps That Are Followed By a colon

Tags:

java

regex

I need a pattern to match words like APPLE: or PEAR:

[A-Z][:] will match the R: but not the whole word and thus gives me a false when I try to match.

Can anybody help?

like image 746
Ian Avatar asked Jul 20 '11 01:07

Ian


People also ask

How do you use A colon in regex?

A colon has no special meaning in Regular Expressions, it just matches a literal colon.

How do you match A word exactly in regular expression?

But if you wish to match an exact word the more elegant way is to use '\b'. In this case following pattern will match the exact phrase'123456'.

How do you match A semicolon in regex?

Semicolon is not in RegEx standard escape characters. It can be used normally in regular expressions, but it has a different function in HES so it cannot be used in expressions. As a workaround, use the regular expression standard of ASCII.

What does the regular expression '[ A za z ]' match?

For example, the regular expression "[ A-Za-z] " specifies to match any single uppercase or lowercase letter. In the character set, a hyphen indicates a range of characters, for example [A-Z] will match any one capital letter.


4 Answers

You want to match one or more capital letter which means you need to use a +. Also your : doesn't need to be in a character class:

[A-Z]+:

like image 108
Paul Avatar answered Oct 03 '22 20:10

Paul


Just add a "quantifier":

/[A-Z]+:/

Note you don't need a character class for a single character.

like image 31
sidyll Avatar answered Oct 03 '22 18:10

sidyll


How about \b[A-Z]+:? The \b is for checking a word boundary btw.

like image 39
shinkou Avatar answered Oct 03 '22 20:10

shinkou


\b can be used to capture characters only in a word-boundary ie between the start and end of a word.

[A-Z] indicates a range of characters and specifying A-Z specifically matches the range of characters from capital A to capital Z. In other words, only upper case letters.

End the query by trying to match a semicolon and you'll find matches of a capital letter word immediately followed by a single semi-colon.

You can use the regular expression in Java like below.

import java.util.regex.*;

public class RegexExample {
  System.out.println(
    Pattern.matches("\b[A-Z]+:", "data: stuff, MIX!: of, APPLE: or, PEAR: or, PineAPPLes: yay!")
  );
}

I recommend finding an online playground for regular expressions. Iterating and experimenting with regexes for a project can be a fast way to learn the limitations and find ways to simplify or improve an expression.

like image 30
SStanley Avatar answered Oct 03 '22 18:10

SStanley