0

I'm currently studying informatics and know what a regex is (not in java so). I have an input like :

"String1  ( Nr = 323) String2 String3 (Nr  = 3)"

I wanted to split it by using:

split("[ ()=]");

because I think this would split all " ","(",")","=". Am i right? or do I need to put a + behind it? and if this is already right I could add a * so I can also split for something like "(("?

If this isn't the problem then my other question regarding regex in java is how can I check if my String only contains numbers.

I tried:

contains(".*\\d+.*")
matches(".*\\d+.*")

But I'm pretty sure one of them is working. So my problem should be with the splitting regex.

My original problem is that I get a NumberFormatException for my splitted String array at the index 2 which normally should be "323"?

Can I use my regex with a *? like "[ ()=]*" ?

Thanks in advance

T.Gounelle
  • 5,953
  • 1
  • 22
  • 32
NhatNienne
  • 937
  • 3
  • 11
  • 20
  • 2
    What do you expect as your output? And to check a string for only digits. `str.matches("\\d+")` – hwnd Feb 19 '15 at 17:58
  • 1
    If you want `323` as index 2 you need to split on one or more of the characters: `split("[ ()=]+")`. – Keppil Feb 19 '15 at 17:59
  • If you intend to extract the numbers from the string and don't care the the other data, you can check `java.util.regex.Pattern` and `Matcher` classes. And this [question/answer](http://stackoverflow.com/questions/2367381/extract-numbers-from-a-string-java) on it. – T.Gounelle Feb 19 '15 at 18:24
  • Not really trying to extract it I just want to know whether the numbers are at the index 2 or 4 (as mentioned in the upvoted answer by hwnd) so I can create my Objects by using splitString[2] or splitString[4] for the number. Not sure if you meant that with extract. – NhatNienne Feb 19 '15 at 18:30

2 Answers2

1

Yes, it will split on those characters but not produce the expected results. You need to use a quantifier with your character class. I recommend using + meaning "one or more" times.

String s = "String1  ( Nr = 323) String2 String3 (Nr  = 3)";
String[] parts = s.split("[ ()=]+");
System.out.println(Arrays.toString(parts));

Output

[String1, Nr, 323, String2, String3, Nr, 3]
hwnd
  • 69,796
  • 4
  • 95
  • 132
  • Thanks now most of the code is working. But there is the possibility for the String to look like: "String1 ( Nr = 323) String2 String3" or "String1 String2 String3 ( Nr = 323)" (323 being an example) I wanted to first check if the splitlength is 5 and then whether the splitstring[2] or from [4] is a number but that doesn't work using ("\\d+"). Any ideas? – NhatNienne Feb 19 '15 at 18:13
  • Oh I think I forgot to tell you guys (gonna edit it) that I can't split for "-". \\s would split for all whitespaces(,.|()/ ....)? edit: what you mean by trimming? (not a native speaker) trim is something like cutting it out? But I don't understand what you mean by trimming the string – NhatNienne Feb 19 '15 at 18:28
0

A regex that is useful for splitting a string

  • must describe all separators between the strings you want to have
  • may not describe the empty string

You have non-space separators ()= surrounded by spaces between the strings you want to have. You could be generous and use

"[ ()=]+"   any mixture

or fiddly (requiring one of the ()=) and do

"\\s*[()=]\\s*"

With this regex, a split of "foo (( bar" would give you three strings.

to make sure it is only *one+ of the trio ()=.

To check whether a string only contains numbers you'll have to define "numbers". There is a general confusion between "digit" and "number", and "number" might be signed or unsigend, integer or fraction,... Numbers implies at least one space.

"\\s*(\\d+)(\\s+\\d+)*\\s*"

describes unsigned integers separated and surrounded by (optional) spaces. In this simple case, also

"[\\s\\d]+"

will do.

laune
  • 31,114
  • 3
  • 29
  • 42