Regular Expression Parse Double

Hammelkeule

I am new to regular expressions. I want to search for NUMBER(19, 4) and the method should return the value(in this case 19,4). But I always get 0 as result !

       int length =0;
       length = patternLength(datatype,"^NUMBER\\((\\d+)\\,\\s*\\)$","NUMBER");

    private static double patternLengthD(String datatype, String patternString, String startsWith) {
        double length=0;
        if (datatype.startsWith(startsWith)) {
            Pattern patternA = Pattern.compile(patternString);
            Matcher matcherA = patternA.matcher(datatype);
                if (matcherA.find()) {
                    length = Double.parseDouble(matcherA.group(1)); 
            }
        }
        return length;
    }
Andreas

You are missing the matching of digits after the comma.
You also don't need to escape the ,. Use this:

"^NUMBER\\((\\d+),\\s*(\\d+)\\)$"

This will give you the first number in group(1) and the second number in group(2).

It is however fairly strict on spaces, so you can be more lenient and match on values like " NUMBER ( 19 , 4 ) " by using this:

"^\\s*NUMBER\\s*\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*\\)\\s*$"

In that case you'll have to drop your startsWith and just use the regex directly. Also, you can remove the anchors (^$) if you change find() to matches().

Since NUMBER(19) is usually allowed too. You can make the second value optional:

"\\s*NUMBER\\s*\\(\\s*(\\d+)\\s*(?:,\\s*(\\d+)\\s*)?\\)\\s*"

group(2) will then return null if the second number is not given.

See regex101 for demo.


Note that your code doesn't compile.
Your method returns a double, but length is an int.

Although 19,4 looks like a floating point number, it is not, and representing it as such is wrong.
You should store the two values separately.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Regular expression for double parentheses

From Dev

Double \\ in regular expression iOS

From Dev

Regular expression to parse url

From Dev

Regular expression to parse data

From Dev

Parse log with regular expression

From Dev

Parse Regular expression in R

From Dev

Double forward slash in regular expression

From Dev

Parse key=value with regular expression

From Dev

Regular expression to parse escape characters

From Dev

Javascript Regular Expression to Parse CSS

From Dev

Regular expression to parse AT command response

From Dev

Regular expression for parse xrandr response

From Dev

unable to parse - in Regular expression in Javascript

From Dev

Regular expression to parse escape characters

From Dev

Regular expression to parse my string

From Dev

Regular expression to parse log file

From Dev

Regular expression to parse configuration file

From Dev

Parse EML text With Regular Expression

From Dev

Parse arithmetic string with regular expression

From Dev

Regular Expression: Double Backslashes in Bracket Expression

From Dev

Regular expression for any double type number

From Dev

Regular expression for double values using QRegExp

From Dev

Regular Expression for anything enclosed in double squar bracket

From Dev

Regular Expression confused by use of double and single quotes

From Dev

Regular expression for double number range validation

From Dev

Regular Expression To Replace Double Slashes with Javascript

From Dev

Flex/Lex: Regular Expression matches double characters

From Dev

Regular Expression for finding double characters in Bash

From Dev

Regular Expression for a character not appearing between double quotes

Related Related

HotTag

Archive