How to extract integer from Java Regex?

0

Pardon my novelty in java, I have the following string ( Below ), I am trying to clean it and extract only the integer digits. What would be the correct java regex to use to achieve my goal:

Original String : uint32_t Count "77 (0x0000004D)"

Desired Output: 77

I have tried reading Java docs on regex but I only got more confused. I guess EE engineers are not cut for this fancy coding tricks :D

java
regex
string
asked on Stack Overflow Apr 13, 2016 by Java_Crawler • edited Apr 15, 2016 by Termininja

2 Answers

0

You could exploit "\\b" which is a word boundary:

String regex = "\\b\\d+\\b";
Matcher m = Pattern.compile(regex).matcher("uint32_t Count \"77 (0x0000004D)\"");
m.find();
System.out.println(m.group()); //output 77  

"\\d+" finds a substring of digits, and surrounding it with "\\b" ensures that it is not embedded in another word/symbol.

answered on Stack Overflow Apr 13, 2016 by Maljam
0

more examples to get a pattern helps but with what you have given i can think of a simple regex that matches the group with the given pattern and then you strip out the quote and get your integer.

(["](\d{1,})) 

I would suggest you play around regex more over here so you learn as you practice

answered on Stack Overflow Apr 13, 2016 by lazydev

User contributions licensed under CC BY-SA 3.0