1
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?

4
  • As an aside, it's worth noting that your problem really doesn't have anything to do with the fact the value is in a map. You're trying to extract the int value 32 from the string "VARCHAR2(32 BYTE)" - where that string comes from is at least somewhat irrelevant. Commented Mar 24, 2016 at 14:24
  • 1
    @KevinEsche: In this case, that would leave 232... Commented Mar 24, 2016 at 14:24
  • @JonSkeet my bad, didn´t really see the 2 in the varchar... Commented Mar 24, 2016 at 14:30
  • I am trying with substring , hope it will work ,its ok . thanks Commented Mar 24, 2016 at 14:31

3 Answers 3

1
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);
    }

  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

In the OP's case, that will return 2 due to the VARCHAR2.
Now this example should print both 2 as well as 32
1

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

2 Comments

Actually it doesn't work. strVal.indexOf("VARCHAR2(") returns 0. So substring method returns "VARCHAR2(32" instead of 32
Correct, I modified it.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.