3

I would like to create a matching pattern for a situation like this

DOMAIN+("Y|A")? 

I would like the matching options to be only

DOMAIN DOMAINY DOMAINA 

but seems like DOMAINX, DOMAINY etc. are matching as well.

3 Answers 3

2

Yes, they are matching because you did not specify that the String needed to end with this. DOMAIN(Y|A)? is matching DOMAINX because it rightfully contains DOMAIN followed by nothing (which is accepted since ? validates 0 or 1 occurence).

You can add this restriction by specifying $ at the end of the regular expression.

Sample code that shows the result of matches. In your full code, you probably want to compile a Pattern instead of doing it each time.

public static void main(String[] args) { String regex = "DOMAIN(Y|A)?$"; System.out.println("DOMAIN".matches(regex)); // prints true System.out.println("DOMAINX".matches(regex)); // prints false System.out.println("DOMAINY".matches(regex)); // prints true System.out.println("DOMAINA".matches(regex)); // prints true } 
Sign up to request clarification or add additional context in comments.

Comments

1

You could use word boundaries, \b, in order to prevent strings such as "DOMAINX" from being matched.

If you just want to handle cases where there are characters after the word, add \b to the end:

DOMAIN(?:Y|A)?\b 

Otherwise, you could place \b around the expression to handle cases where there may be characters at the start/end:

\bDOMAIN(?:Y|A)?\b 

I also made (?:Y|A) a non-capturing group and I removed the quotes.

See the matches here.


However, as your title implies, if you only want to handle characters at the end of a line, use the $ anchor at the end of your expression:

DOMAIN(?:Y|A)?$ 

You may have to add the m (multi-line) flag so that the anchor matches at the start/end of a line rather than at the start/end of the string:

(?m)DOMAIN(?:Y|A)?$ 

Comments

0

You need this

DOMAIN(Y|A)? 

If you need it to be a word in text you should anchor it with \b as Josh shows.

Your regex does the following

DOMAIN+("Y|A")?

DOMAIN+("Y|A")? 

Options: Case sensitive; Exact spacing; Dot doesn’t match line breaks; ^$ don’t match at line breaks; Regex syntax only

  • [Match the character string “DOMAI” literally (case sensitive)][1] DOMAI
  • [Match the character “N” literally (case sensitive)][1] N+
    • [Between one and unlimited times, as many times as possible, giving back as needed (greedy)][2] +
  • [Match the regex below and capture its match into backreference number 1][3] ("Y|A")?
    • [Between zero and one times, as many times as possible, giving back as needed (greedy)][4] ?
    • [Match this alternative (attempting the next alternative only if this one fails)][5] "Y
      • [Match the character string “"Y” literally (case sensitive)][1] "Y
    • [Or match this alternative (the entire group fails if this one fails to match)][5] A"
      • [Match the character string “A"” literally (case sensitive)][1] A"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.