-2

I've a number of subscription from a YouTube channel that I copied. It's "4 372 236". I'm testing the "\s+" regex on https://regex101.com for that number and it does not work. When i'm writing the same number on my own the regex does work. Anybody knows what's wrong?.

I'm trying to remove the white space chars from such numbers but i cannot do it. I tried also the .replaceAll(" ", "") method but does not work neither.

screen from regex101.com

The JSON Youtube code: JSON Youtube

Then I'm using JSON library to get the subscriptions like this:

JSONObject jsonObject = new JSONObject();
    jsonObject = new JSONObject(content);
    JSONArray tabs = jsonObject.getJSONObject("contents")
            .getJSONObject("twoColumnBrowseResultsRenderer")
            .getJSONArray("tabs");
    JSONObject tabRenderer = tabs.getJSONObject(5).getJSONObject("tabRenderer");
    JSONObject sectionListRenderer = tabRenderer.getJSONObject("content").getJSONObject("sectionListRenderer");
    JSONArray contents2 = sectionListRenderer.getJSONArray("contents");
    JSONObject itemSectionRenderer = contents2.getJSONObject(0).getJSONObject("itemSectionRenderer").getJSONArray("contents").getJSONObject(0);
    JSONObject channelAboutFullMetadataRenderer = itemSectionRenderer.getJSONObject("channelAboutFullMetadataRenderer");

    String subs = channelAboutFullMetadataRenderer.getJSONObject("subscriberCountText").getJSONArray("runs").getJSONObject(0).getString("text");

And finally, i'm using the regex to delete the whitespaces from number:

subs = subs.replaceAll("\\s+", "");
System.out.println(subs);

I tried this too but it does not work. I think it's not a regular space but I don't know how to recognise it.

 subs = subs.replaceAll(" ", "");
Jim.D
  • 17
  • 7

4 Answers4

0

I suggest you copy that number into notepad++ and use the "show all symbols" option. Maybe there are not only whitespaces inbetween.

enter image description here

EDIT: sorry for not using the comment function, i need reputation for that and it sucks.

metters
  • 533
  • 1
  • 8
  • 17
0

You need to escape the backslash:

System.out.println("4 372 236".replaceAll("\\s+", ""));

prints: 4372236

jste89
  • 426
  • 4
  • 15
0

Your question doesn't really explain what you are trying to accomplish and you have not provided any sort of code other than a method to go off. It really depends on what your end goal is.

Generally, when you are trying to do can be accomplished easily through the replaceAll method as mentioned.

String test = "4 372 236";
String reg  = "\\s+";
String newLine = test.replaceAll(reg, "");

or simply

String test = "4 372 236";
String newLine = test.replaceAll(" ", "");
WiththeWind
  • 153
  • 1
  • 9
0

Okay guys, I found it out. It was not a duplication of Why does String.replace not work?. I kept in my mind that string in Java are immutable.

Between the numbers there are not simple spaces. It's NO-BREAK SPACE' (U+00A0). So, the regex should look like this

 subs = subs.replaceAll("[\\u202F\\u00A0]", "");

Maybe it will help somebody in the future :) Thanks @metters

Jim.D
  • 17
  • 7