简体   繁体   中英

how to extract or get int vaue from string in a hashmap value

HashMap<String,String> dataRules = new HashMap<String,String>();

dataRules.put("FAX_GW_NW_ELE_DM_","VARCHAR2(32 BYTE)");

In the above given expression I need to extract 32 BYTE as int 32 from value to use. Can anyone suggest how I can do this?

package general;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MatchNumber {

    public static ArrayList<Integer> extractInt(String mapValue) {
        Matcher matcher = Pattern.compile("\\d+").matcher(mapValue);

        //  throw new NumberFormatException("For input string [" + mapValue + "]");
        ArrayList<Integer> array = new ArrayList<Integer>();
        while (matcher.find()){
            array.add(Integer.parseInt(matcher.group()));
        }
        return array;
    }

public static void main(String[] args){
    ArrayList<Integer> result = extractInt("VARCHAR2(32 BYTE)");
    for (int i : result){
        System.out.println(i);
    }

  }
}

Assuming you have the fixed prefix and sufix you can extract it like this

Integer.valueOf(strVal.substring(9,strVal.indexOf("BYTE)") ))

where 9 is length of "VARCHAR2(" plus 1

in case the the type value, as varchar2, will differ, but the number will be allways written in paranthesis you could use String#replaceAll with a capturing group to extract the number.

String input = "VARCHAR2(32 BYTE)";
System.out.println(input.replaceAll(".*\\((\\d*).*", "$1"));

output

32

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM