0

Here's what I want, there's a string like, CHAR_27[19;50H and I want to get the numbers from the inside separately.

// CHAR_27 = Character.toString( (char)27 );
// Or "\u001B"
String escapeString = "CHAR_27[19;50H";
String coord = escapeString.replaceAll("[^0-9;]", "");
List<String> coords = Splitter.on(";").splitToList(coord);

if (coords.size() != 2) continue;

rowIndex = Integer.parseInt(coords.get(0)) - 1;
colIndex = Integer.parseInt(coords.get(1)) - 1;

But sometimes it throws a NumberFormatException, invalid integer CHAR_27[19.

Mostly it works fine.

Is there something wrong with my regex?

Or should I use StringUtils.removePattern() for a more consistent result?

I got this report from Nokia_N1 and GT-810(rolex). Both of them are Android 5.1.

And I'm using Android API 24.

Kimi Chiu
  • 2,103
  • 3
  • 22
  • 37
  • 2
    Your pattern looks fine if you want to remove everything except `0-9` and `;`. It is hard to tell what is wrong without seeing [SSCCE](http://sscce.org) / [MCVE]. Maybe at some part of your code you simply forgot to assign result of `replaceAll` back to variable, like described here: http://stackoverflow.com/questions/12734721/string-not-replacing-characters – Pshemo Oct 25 '16 at 02:35
  • But I just want to know why "the same pattern with the same source returns different results", just like the title describes. Is there any exception or something I'm missing? It is clear enough in my question, there are code example and two different output. – Kimi Chiu Oct 25 '16 at 02:44
  • Maybe problem is related to context. Like I said, it is hard to tell what could go wrong without simple, but full example which would let us reproduce this incorrect result. According to Java rules this shouldn't happen and I am not aware of any bug within Pattern/Matcher classes which are used internally here (assuming that Android is doing things [same way as Java does](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8-b132/java/lang/String.java#String.replaceAll%28java.lang.String%2Cjava.lang.String%29)). – Pshemo Oct 25 '16 at 02:53
  • 1
    But if it is a bug, it could be helpful to provide more info about tools which you are using like android version and IDE. – Pshemo Oct 25 '16 at 02:59
  • Thanks for the advice, I've edited my question. – Kimi Chiu Oct 25 '16 at 03:03
  • Can the input string in rare cases contain **line breaks** (like `\r`, `\n` or any combination of those)? – slartidan Oct 25 '16 at 11:57
  • I don't think so. I've used a pattern to check the format of the escapeString. It should always look like "CHAR_27[19;50H". What happens if it contained line breaks? – Kimi Chiu Oct 27 '16 at 04:22

1 Answers1

0

The code that you posted will not rise the mentioned exception (if the input is a single line string).

However your code contains some vulnerabilities:

  • Orininally separate numbers can be joined by the expression
  • Empty strings can be created

I would recommend to use:

String escapeString = "CHAR_27[19;50H";
String[] coords = escapeString.split("[^0-9]+");

if (coords.length != 2) continue;

rowIndex = Integer.parseInt(coords[0]) - 1;
colIndex = Integer.parseInt(coords[1]) - 1;
slartidan
  • 20,403
  • 15
  • 83
  • 131
  • Thanks for the suggestion. But I've used a Pattern to check the format of the escapeString. And I did get this report from the user. I saw this on google play developer console. I just can't figure out why it's not replaced. – Kimi Chiu Oct 25 '16 at 11:14