How to check if the first character of a string is a number?

4
Either I make a stupid mistake in the regex or there is a bug in the ismatch function. I would like to check if the first character of a string is a number. The regex I used is this: ^[0-9]. So the check I did is this one is isMatch($SomeString,'^[0-9]') Now the strange part. If the string is 12 it returns false. It will return true only if the string consist of one character. So it will return false with 1a, 2b or 124 and return true when the string is only one number like 1 or 2. Now I can easily work around it by just cheching the first character and not the whole string, but it is not what I would have expected. Or am I making some stupid mistake here. Regards, Ronald [EDIT] I could be wrong here, but from the definition of regex metacharacters: ^ Matches the starting position within the string. In line-based tools, it matches the starting position of any line. [^ ] Matches a single character that is not contained within the brackets. For example, [^abc] matches any character other than "a", "b", or "c". [^a-z] matches any single character that is not a lowercase letter from "a" to "z". Likewise, literal characters and ranges can be mixed. So ^[0-9] has a different meaning then [^0-9]. The first just looks at the starting position of the string while the second indeed only matches a singel character. So I think that Bas answers is wrong. He would be right if I had used [^0-9]. The problem is that when I test my regex is a couple of different regex testers they all match, while when I feed it into the ismatch I get the opposite answer..... [EDIT2] Finally found the answer on this website : http://www.regular-expressions.info/java.html So Michel Weststrate is right. Mendix does it the Java way WHICH DIFFER FROM ALL OTHER REGEX LIBRARIES. Something for the documentation. Quick Regex Methods of The String Class The Java String class has several methods that allow you to perform an operation using a regular expression on that string in a minimal amount of code. The downside is that you cannot specify options such as "case insensitive" or "dot matches newline". For performance reasons, you should also not use these methods if you will be using the same regular expression often. myString.matches("regex") returns true or false depending whether the string can be matched entirely by the regular expression. It is important to remember that String.matches() only returns true if the entire string can be matched. In other words: "regex" is applied as if you had written "^regex$" with start and end of string anchors. This is different from most other regex libraries, where the "quick match test" method returns true if the regex can be matched anywhere in the string. If myString is abc then myString.matches("bc") returns false. bc matches abc, but ^bc$ (which is really being used here) does not.
asked
2 answers
1

The correct answer is in the comments, by Bas and Achiel, so I'm just repeating for clarity sake:

Use '^[0-9].$', because isMatch will only yield only true if the *complete string satisfies the expression, and not just a part of it. So you also need to capture the remainder of the string

answered
1

How about this regex does this do what you want?

http://regexr.com/3anj3

answered