Question: I have some titles as follows. I need to extract only numbers after 'Bug' with regular expression.

```
Bug 1234 - description
Bug1234 - description
Bug 1234  description
BUG 1234 - description
Bug 1234: description
Bugxxxx: description

```

The character after the numbers can be any non-character that I would expect. I noticed that in our bug titles, I see three non-characters, `':', '-', ' '`. 

Is there a way to do it with java regular expression just to extract numbers?

The last usecase 'Bugxxxx' should be an empty string returned, because 'xxxxx' is not numeric.

Thanks.

Answer: 
You can use this regex `Bug\\s?(\\d*)` with the Pattern.CASE\_INSENSITIVE flag to extract the number. It will be in the first group. For example

```
String foo = "Bug 1234 - description";
Pattern pattern = Pattern.compile("Bug\\s?(\\d*)", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(foo);
if (matcher.find()) {
    System.out.println(matcher.group(1));
}

```
You can follow below:

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

public class Solution {

    public static void main(String[] args) {
        String subject = "Bug 1234 - description";
        Pattern bugNumber = Pattern.compile("(?i)bug\\s*([0-9]+)");

        // find the bug#
        Matcher matcher = bugNumber.matcher(subject);
        while (matcher.find()) {
            System.out.println(matcher.group(1));
        }
    }

}

```