I have a String that looks like this
String = "Förpackning Flaska (375 ml) Förslutning Skruvkapsyl Kr/lit (104,00) Pris 39,00 kr Antal i butik 30 st Hyllplats 04-11-01 Förpackning Flaska (750 ml) Förslutning Plastkork/syntetkork Kr/lit (100,00) Pris 75,00 kr Antal i butik 16 st Hyllplats 02-03-01";
I want to extract some of the text inside this string.
The end result I want is this:
“Förpackning Flaska (375 ml) Antal i butik 30 st Förpackning Flaska (750 ml) Antal i butik 16 st”.
I can use the following code:
name = name.replace(name.substring(name.indexOf(") ") + 2, name.indexOf("Antal")), "");
name = name.replace(name.substring(name.indexOf("st ") + 2, name.lastIndexOf("")), "");
That will give me this result:
“Förpackning Flaska (375 ml) Antal i butik 30 st”
It basically does what I want it to do, but it stops after the first occurance of the pattern.
I have tried to use a regex pattern but I can’t get it to work. From observing the string, I have concluded that I need a regex pattern that matches everything between “) ” and “Antal”. I will also need to remove the other clutter, but that is easy. My problem is that I can’t seem to get my regex to work, and that would probably be the best way to do something like this. I know that I have to escape the paranthesis to make it a literal character in my regex, but I just can’t get it to work.
This is the regex I’ve come up with and tried:
Pattern p = Pattern.compile("\b\\) (.+?)\bAntal");
Matcher m = p.matcher(name);
m.find();
System.out.println(m.group(1));
Any help and ideas are welcome!
This can be done in one line!
It looks like you want to remove:
"st", and")"and"Antal"Here’s the code that will do that:
Notes regarding the regex:
"\b". This is a mistake – you cded a literal backspace. Instead, you code it as"\\b"(A|B)to match both in one regex?in".*?"is important – it means a non-greedy match. Without it, it will match the first bracket and the lastAntal, skipping over anyAntalbetweenHere’s some test code:
Output: