Match a string containing a comma (eg 1,5)

java, regex

Solution

The caret `^` matches the beginning of the text. `[,]+` matches an arbitrary number of commas. What you need is something to ignore everything before and after the item you are looking for:

^.*[,].*$

The dot matches any character except newline. `*` is repetition (0 -- any number). The `$` matches the end of text.

Note that trimming the string is unnecessary here.

Problem

i want to check if the string i take from a Textfield has a comma, in order to drop it and ask for a new value with dot. i try to use ``` textTestAr.trim().matches("^[,]+$") ``` but nothing happens, while this ``` ^[1-9,]+$ ``` does the trick, but also matches numbers like 1.

Original source