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

I have a problem with java regex.

how can I find a pattern of 1 or more numbers followed by a single. in line?

+4
source share
5 answers

I think this is the answer to your question:

String searchText = "asdgasdgasdg a121341234.sdg asdg as12..dg a1234.sdg "; searchText.matches("\\d+\\.[^.]"); 

This will correspond to "121341234." and "1234." but not "12."

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

\\d represents any digit + says one or more

See http://www.vogella.com/articles/JavaRegularExpressions/article.html

+2
source

In a regular expression, the \d metacharacter is used to represent an integer, but to represent it in Java code, you need to use \\d as a regular expression because of the double analysis performed on them.

First, a string parser that converts it to \d , and then a regex parser that will interpret it as an integer metacharacter (which we want).

For "one or more" parts, we use + greedy quantifier.

For presentation . we use \\. due to double parsing scenario.

So in the end we have (\\d)+(\\.) .

+1
source

\\d+)\\.

\\d for numbers, + for one or more, \\. for a point. If . written without a backslash before it, it matches any character.

0
source

All Articles