user840930
user840930

Reputation: 5578

java regex: find pattern of 1 or more numbers followed by a single

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

Answers (5)

Surender Thakran
Surender Thakran

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

Jigar Joshi
Jigar Joshi

Reputation: 240928

(\\d)+\\.

\\d represents any digit
+ says one or more

Refer this http://www.vogella.com/articles/JavaRegularExpressions/article.html

Upvotes: 3

ioseb
ioseb

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

Whimusical
Whimusical

Reputation: 6649

"^[\\d]+[\\.]$"

^     = start of string
[\\d] = any digit
+     = 1 or more ocurrences
\\.   = escaped dot char
$     = end of string   

Upvotes: 11

AlexR
AlexR

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

Related Questions