Reputation: 5578
I'm having a java regex problem.
how can I find pattern of 1 or more numbers followed by a single . in a string?
Upvotes: 13
Views: 69280
Reputation: 4054
In regex the metacharacter \d
is used to represent an integer but to represent it in a java code as a regex one would have to use \\d
because of the double parsing performed on them.
First a string parser which will convert it to \d
and then the regex parser which will interpret it as an integer metacharacter (which is what we want).
For the "one or more" part we use the +
greedy quantifier.
To represent a .
we use \\.
because of the double parsing scenario.
So in the end we have (\\d)+(\\.)
.
Upvotes: 1
Reputation: 240928
(\\d)+\\.
\\d
represents any digit
+
says one or more
Refer this http://www.vogella.com/articles/JavaRegularExpressions/article.html
Upvotes: 3
Reputation: 16951
I think this is the answer to your question:
String searchText = "asdgasdgasdg a121341234.sdg asdg as12..dg a1234.sdg ";
searchText.matches("\\d+\\.[^.]");
This will match "121341234." and "1234." but not "12."
Upvotes: 8
Reputation: 6649
"^[\\d]+[\\.]$"
^ = start of string
[\\d] = any digit
+ = 1 or more ocurrences
\\. = escaped dot char
$ = end of string
Upvotes: 11
Reputation: 115378
\\d+)\\.
\\d
is for numbers, +
is for one and more, \\.
is for dot. If .
is written without backslash before it it matches any character.
Upvotes: 0