Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - regular expression for get number format

Tags:

java

string

regex

I have this:

  • 110121 NATURAL 95 1570,40
  • 110121 NATURAL 95 1570,40*
  • 41,110 1 x 38,20 CZK)[A] *
  • ' 31,831 261,791 1308,61)
  • >01572 PRAVO SO 17,00
  • 1,000 ks x 17,00
  • 1570,40

Every line of this output is saved in List and I want to get number 1570,40

My regular expressions looks like this for this type of format

    "([1-9][0-9]*[\\.|,][0-9]{2})[^\\.\\d](.*)"
    "^([1-9][0-9]*[\\.|,][0-9]{2})$"

I have a problem that 1570,40 at the last line if founded (by second regular expression), also 1570,40 (from line with 1570,40* at the end) but the first line is not founded.. do you know where is the problem?

like image 630
Jan Bouchner Avatar asked Apr 23 '13 11:04

Jan Bouchner


1 Answers

Not sure I well understand your needs, but I think you could use word boundaries like:

\b([1-9]\d*[.,]\d{2})\b

In order to not match dates, you can use:

(?:^|[^.,\d])(\d+[,.]\d\d)(?:[^.,\d]|$)

explanation:

The regular expression:

(?-imsx:(?:^|[^.,\d])(\d+[,.]\d\d)(?:[^.,\d]|$))

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    ^                        the beginning of the string
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    [^.,\d]                  any character except: '.', ',', digits
                             (0-9)
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
----------------------------------------------------------------------
    [,.]                     any character of: ',', '.'
----------------------------------------------------------------------
    \d                       digits (0-9)
----------------------------------------------------------------------
    \d                       digits (0-9)
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    [^.,\d]                  any character except: '.', ',', digits
                             (0-9)
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
like image 156
Toto Avatar answered Sep 28 '22 10:09

Toto