0

I want to extract numeric values from a string.

eg. I have values like this:

abc9856412368def            
9812345678abc           
abc9812345678       
abc256789def      
4567abc    

I want Output like this(in three columns):

Col1         Col2            Col3
abc def    9856412368
abc        9812345678 
abc        9812345678
abc def                     256789
abc                         4567

If number is 10 or more than 10 then store it into col2 else in col3. I've tried using REGEXP but it doesn't extract the number in mysql workbench. But it returned zero rows.

select first_name
from mytable
where First_Name regexp '^[0-9]+$' and First_Name!=""

I am thinking to find it using java. Stored it in resultset. I also want to make sure that mobile numbers should be inserted into Column 2. Can anyone guide me for this?

1 Answer 1

1

I think also in Java Regex should be the way to go.

// Extract numbers from String
Pattern p1 = Pattern.compile("-?\\d+");
// Extract words from String
Pattern p2 = Pattern.compile("-?\\D+");

Matcher m1 = p1.matcher("abc9856412368def");
Matcher m2 = p2.matcher("abc9856412368def");
while (m1.find()) {
  System.out.println(m.group()); // Prints 9856412368
}
while (m2.find()) {
  System.out.println(m.group()); // Prints abc and def
}

Now you only need to format the output.

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

1 Comment

Thank you for your help.But I've a huge data, number within a string can be in anywhere.I need to fetch such numbers from the whole string. and insert them into another columns.

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.