1

I would like to filter out all words containing 1 number and 3 capital letters with a total length of 4. See my example here: http://gskinner.com/RegExr/?32taa

ArrayList<String> result = new ArrayList<String>();


for(int i = 0; i <= 10; i++){

    String message = resp.data.get(i).message;

    Matcher m = MY_PATTERN.matcher("\b(?=[^\d]*\d[^\d]*)[A-Z\d]{4}\b");

        while (m.find()) {
            String s = m.group(1);
            result.add(s);
        }
}

But when i pass my regexp pattern to the matcher method, i get the error:

Invalid escape sequence (valid ones are  \b  \t  \n  \f  \r  \"  \'  \\ )

Do I need to swap \d with another letter?

Johan
  • 35,120
  • 54
  • 178
  • 293

3 Answers3

5

Regex don't go well with String...

So u need to use \\d instead of \d

- When you write \ java expects either n or b or t or a and few others... after it, but when you give d it gets the shock of its life, and think what the hell.. i don't know nothing about \d, So we need to tell java that \ should be taken literally by it instead of expecting it as escape character.

- In the case of . (dot) it becomes even more complicated, when you give "." java takes it literally but its a regex so you need to make it look like that so you prefix it with \, so it becomes \. , now again the same problem as the earlier one begins as now java accepts n or b etc after \ but it gets a ".", so we again prefix it with another \, so now it becomes \\.

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
4

In Java, you need to escape the backslash with an extra backslash, when representing the pattern in string.

So, \b should be \\b, and \d should be \\d.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
3

Your code has two issues:

  • Your pattern is wrong, as it allows more digits - see http://regexr.com?32u3e

  • Java requires double escape slashes...

Use regex pattern

\\b(?=[A-Z]*\\d[A-Z]*\\b)[A-Z\\d]{4}\\b
Ωmega
  • 42,614
  • 34
  • 134
  • 203