AyhamSYR
AyhamSYR

Reputation: 13

How to match a string in this way?

I need to check if a String matches this specific pattern.

The pattern is:

(Numbers)(all characters allowed)(numbers)

and the numbers may have a comma ("." or ",")!

For instance the input could be 500+400 or 400,021+213.443.

I tried Pattern.matches("[0-9],?.?+[0-9],?.?+", theequation2), but it didn't work!

I know that I have to use the method Pattern.match(regex, String), but I am not being able to find the correct regex.

Upvotes: 0

Views: 62

Answers (2)

The fourth bird
The fourth bird

Reputation: 163577

In your regex you have to escape the dot \. to match it literally and escape the \+ or else it would make the ? a possessive quantifier. To match 1+ digits you have to use a quantifier [0-9]+

For your example data, you could match 1+ digits followed by an optional part which matches either a dot or a comma at the start and at the end. If you want to match 1 time any character you could use a dot.

Instead of using a dot, you could also use for example a character class [-+*] to list some operators or list what you would allow to match. If this should be the only match, you could use anchors to assert the start ^ and the end $ of the string.

\d+(?:[.,]\d+)?.\d+(?:[.,]\d+)?

In Java:

String regex = "\\d+(?:[.,]\\d+)?.\\d+(?:[.,]\\d+)?";

Regex demo

That would match:

  • \d+(?:[.,]\d+)? 1+ digits followed by an optional part that matches . or , followed by 1+ digits
  • . Match any character (Use .+) to repeat 1+ times
  • Same as the first pattern

Upvotes: 0

KevinO
KevinO

Reputation: 4403

Dealing with numbers can be difficult. This approach will deal with your examples, but check carefully. I also didn't do "all characters" in the middle grouping, as "all" would include numbers, so instead I assumed that finding the next non-number would be appropriate.

This Java regex handles the requirements:

"((-?)[\\d,.]+)([^\\d-]+)((-?)[\\d,.]+)"

However, there is a potential issue in the above. Consider the following: 300 - -200. The foregoing won't match that case.

Now, based upon the examples, I think the point is that one should have a valid operator. The number of math operations is likely limited, so I would whitelist the operators in the middle. Thus, something like:

"((-?)[\\d,.]+)([\\s]*[*/+-]+[\\s]*)((-?)[\\d,.]+)"

Would, I think, be more appropriate. The [*/+-] can be expanded for the power operator ^ or whatever. Now, if one is going to start adding words (such as mod) in the equation, then the expression will need to be modified.

You can see this regular expression here

Upvotes: 1

Related Questions