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.
The 0-9 indicates characters 0 through 9, the comma , indicates comma, and the semicolon indicates a ; . The closing ] indicates the end of the character set. The plus + indicates that one or more of the "previous item" must be present.
Special Regex Characters: These characters have special meaning in regex (to be discussed below): . , + , * , ? , ^ , $ , ( , ) , [ , ] , { , } , | , \ . Escape Sequences (\char): To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ).
Example : ^\d{3} will match with patterns like "901" in "901-333-". It tells the computer that the match must occur at the end of the string or before \n at the end of the line or string. Example : -\d{3}$ will match with patterns like "-333" in "-901-333". A character class matches any one of a set of characters.
Alternation is the term in regular expression that is actually a simple “OR”. In a regular expression it is denoted with a vertical line character | . For instance, we need to find programming languages: HTML, PHP, Java or JavaScript.
You don't need to force regular expressions on this problem. A simple textTestAr.indexOf(',')
will do.
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.
Why not use String#contains instead of regex here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With