1

This seems like a basic thing but i can't seem to get my head around regex's I've never really used them before and now i have come across a time they would be useful.

I've looked at examples and past questions for the last hour and still don't understand it. my problem is I have a string

"(2 h 9 min from now) | +18.7 feet"

which i want to split into two strings

String a = "2 h 9 min from now";

and

String b = "18.7 feet";

How can i split the string using a regex and use the 'regex's' in other strings?

so far i have come up with:

stringx.split("(%s) | +%s \n");

and

stringx.split("(\\w) | +\d.\d feet");

but i dont know how to get %s (if thats even right) into a string outside of the regex

1
  • You seem to be confusing split with regex pattern matching. Commented Oct 16, 2013 at 20:51

4 Answers 4

2

As you want to remove some chars (the () and +), the safest approach is standard regex matching with Pattern and Matcher classes:

public static void main (String[] args) {
    String input= "(2 h 9 min from now) | +18.7 feet";
    System.out.println("Input: "+ input);
    Pattern p = Pattern.compile("\\(([^)]+)\\) \\| \\+(\\d+\\.\\d feet)");
    Matcher m = p.matcher(input);
    String a = null, b = null;
    if (m.find()) {
        a = m.group(1);
        b = m.group(2);
    }
    System.out.println("a: "+ a);
    System.out.println("b: "+ b);
}

Output:

Input: (2 h 9 min from now) | +18.7 feet
a: 2 h 9 min from now
b: 18.7 feet

See online demo here.

Sign up to request clarification or add additional context in comments.

6 Comments

Note: If you really want to use String#split(), you can use the code in this demo. Although I wouldn't recommend it, as the slightest change in the input may cause unexpected output, and also your intent is not clear in the code (what would make the job of whoever maintains it - maybe yourself in the future - much harder).
It still looks messy using regex's. It is a segment of information taken from a bufferedReader from a url api. exept from string.replace the string.split what else would you recommend I use? Edit - yes I know strickly speaking I should use a xml parser however I couldnt get one to work correctly and seeing that I could do the same in 20 lines (60 lines less than the parser) I wanted to explore other options of making it more efficient and compacting it further
I'd still suggest the use of regexes (and the Pattern/Matcher classes). I know it looks a bit messy, but your scenario really is a use case for regexes. Other approaches, such as split() or replace(), may send the wrong message (they can make the code harder to read). Nevertheless, using regexes, you can make the code much less messy using methods. For example, check this demo. IMO, it is very clean.
Aw, if you're aiming for less lines of code, the split() example (click here to see it) is unbeatable, although everyone comes close.
Yeah I just wanted to explore other options, just out of curiosity is there some if any difference in speed or is it just a matter of milliseconds?
|
0
StringTokenizer stringtokenizer = new StringTokenizer("Your string", "|");
while (stringtokenizer.hasMoreElements()) {
System.out.println(stringtokenizer.nextToken());
}

Comments

0

You can use:

String s = "(2 h 9 min from now) | +18.7 feet";
Pattern p = Pattern.compile("^\\(([^)]+)\\)\\s*\\|\\s*\\+(.*)$");
Matcher m = p.matcher(s);
if (m.find())               
    System.out.println(m.group(1) + " :: " + m.group(2)); 

 // 2 h 9 min from now :: 18.7 feet

1 Comment

The OP wants to get rid of the brackets and plus sign too it seems.
0

I would do that in two steps.

  • First, you split
  • Then, you sanitize

For instance:

// the original text
String text = "(2 h 9 min from now) | +18.7 feet";
// splitting on the "|" separator
String[] splitted = text.split("\\|");
// printing the raw "split" array
System.out.println("Raw: " + Arrays.toString(splitted));
// iterating over the raw elements of the array
for (String split: splitted) {
    // replacing all "raw" strings with the group composed of 
    // word characters in between non word characters (if any)
    System.out.println(split.replaceAll("^\\W*(.+?)\\W*$", "$1"));
}

Output:

Raw: [(2 h 9 min from now) ,  +18.7 feet]
2 h 9 min from now
18.7 feet

Not the cleanest solution, but it'll give you a start.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.