user1452030
user1452030

Reputation: 1021

Java Regular expression - Pattern matching on a pipe-delimited string

This is pretty basic, but I'm not sure what I'm missing. I'm trying to do a pattern match on a pipe-delimited string, to ensure that I can process it accurately (checking a pre-condition).

Input strings will be of the form: IMAGE|{HEIGHT}|{WIDTH}|{ASPECTRATIO} Example: IMAGE|1080|1920|16x9

I'm trying to validate if the string is in this format with the regular expression:

IMAGE\\|[0-9]?\\|[0-9]?\\|[0-9]?x[0-9]?"

But:

String pattern = "IMAGE\\|[0-9]?\\|[0-9]?\\|[0-9]?x[0-9]?";
System.out.println("IMAGE|1080|1920|16x9".matches(pattern));

returns false. What am I missing?

Upvotes: 1

Views: 1179

Answers (1)

Mark A. Fitzgerald
Mark A. Fitzgerald

Reputation: 1249

I believe you mean to use + to indicate one or more of where you're using ? to indicate 0 or 1 of.

Upvotes: 3

Related Questions