-2
String ="(Buy) 655500 - (Sell) 656500";

I want to split this string by ignoring (Buy), -, (Sell).

The final result I want is like this 655500 656500.

Above is the example..Actually my string contain UTF-8 characters..But I left it here

lynndragon
  • 370
  • 2
  • 12

6 Answers6

4

Regular Expression

    String src = "(Buy) 655500 - (Sell) 656500";
    String reg = "[0-9]+";
    Pattern pattern = Pattern.compile(reg);
    Matcher matcher = pattern.matcher(src);
    while(matcher.find()) {
       System.out.println(matcher.group());
    }
Longwayto
  • 396
  • 2
  • 6
  • I think this is the best compatible with my code..By using regular expression, this is the best for UTF8 character condition..Thanks Longwayto – lynndragon Dec 16 '13 at 09:15
1
String string = "(Buy) 655500 - (Sell) 656500";

String needed = string.replaceAll("[\"(Buy)(Sell)-]", "");

this should work maybe ... needed is the String that should give you the needed result.

Rakeeb Rajbhandari
  • 5,043
  • 6
  • 43
  • 74
0

Your best approach would be to define a regular expression pattern match for the sequence and extract the values from that.

Using Regular Expressions to Extract a Value in Java

Community
  • 1
  • 1
Tim B
  • 40,716
  • 16
  • 83
  • 128
0

If your syntax is always like that you can just split like that :

String string = "(Buy) 655500 - (Sell) 656500";
String replaced= string.replaceAll("[(Buy)(Sell)]", "");
String[] values = replaced.split("-");

here : value[0] will be 655500 and values[1] will be 656500

If Your requirement is different then comment.

Cropper
  • 1,177
  • 2
  • 14
  • 36
0

another way:

String baseStr = "(Buy) 655500 - (Sell) 656500";  
String buy = baseStr.split("-")[0].replaceAll("\\D+", "");  
String sell = baseStr.split("-")[1].replaceAll("\\D+", "");  

System.out.println("Base String: " + baseStr);  
System.out.println("Buy String : " + buy);  
System.out.println("Sell String: " + sell);  

and here's the output:

Base String: (Buy) 655500 - (Sell) 656500  
Buy String : 655500  
Sell String: 656500  
A-SM
  • 882
  • 2
  • 6
  • 18
0

Try this:

    String text = "(Buy) 655500 - (Sell) 656500";
    List<String> parts = new LinkedList<String>(Arrays.asList(text.split("\\D")));
    parts.removeAll(Arrays.asList(""));
    System.out.println(parts);

Fnd you will get list of numbers in your string.