Skip to content
Advertisement

Java: Extracting a specific REGEXP pattern out of a string

How is it possible to extract only a time part of the form XX:YY out of a string?

For example – from a string like:

sdhgjhdgsjdf12:34knvxjkvndf, I would like to extract only 12:34.

( The surrounding chars can be spaces too of course )

Of course I can find the semicolon and get two chars before and two chars after, but it is bahhhhhh…..

Advertisement

Answer

You can use this look-around based regex for your match:

JavaScript

RegEx Demo

In Java:

JavaScript

RegEx Breakup:

JavaScript

Full Code:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement