Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Java Regular Expressions (regex) Greif

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Regular Expressions (regex) Greif

    Hi, I'm trying to parse some text with java's regex and nothing seems to be working -_-;;;
    In this code all I am trying to do is parse a string for words and it doesn't work:
    Code:

    Pattern pattern = Pattern.compile(".*(\\w+).*");
    Matcher matcher = pattern.matcher(buffer);

    To get the split string I am trying both this:

    					String[] matches = pattern.split(buffer);
    					System.out.println("split produced:");
    					for(int i = 0; i < matches.length; i++){
    						System.out.println(matches[i]);
    					}

    and this:

    					matcher.find(0);
    					System.out.println(matcher.group());
    					while(matcher.find()){
    						System.out.println(matcher.group(1));
    					}

    What I am tyring to do is split a string like this:
    Time IRIGB (s) Lat Ltn (deg) Lon Ltn (deg) Tas stbd (m/s) wind speed Hg (m/s) wind direction Hg (deg t)

    into substrings like this:
    Time IRIGB (s)
    Lat Ltn (deg)
    Lon Ltn (deg)
    Tas stbd (m/s)
    wind speed Hg (m/s)
    wind direction Hg (deg t)


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Java Regular Expressions (regex) Greif

    er could you not do something like
    s.split(".*(.*)");

    Nooo, you could not lol! nvm sorry


    Chris
    Last edited by Freaky Chris; June 10th, 2009 at 04:47 PM.

  3. #3
    Junior Member
    Join Date
    Jun 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Regular Expressions (regex) Greif

    I found the answer, this works:

    				if(matcher.find()){
    					System.out.println("found:");
    					System.out.println(matcher.group());
    					while(matcher.find()){
    						System.out.println(matcher.group());
    					}
    				}else{
    					JOptionPane.showMessageDialog(this, "File not formatted correctly.");
    					return;
    				}

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Java Regular Expressions (regex) Greif

    Quote Originally Posted by username9000 View Post
    I found the answer, this works:

                    if(matcher.find()){
                        System.out.println("found:");
                        System.out.println(matcher.group());
                        while(matcher.find()){
                            System.out.println(matcher.group());
                        }
                    }else{
                        JOptionPane.showMessageDialog(this, "File not formatted correctly.");
                        return;
                    }
    Hello username9000. Welcome to the Java Programming Forums.

    I couldn't get the required results using the regular expression you posted. Is this all you changed to get it to work?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #5
    Junior Member
    Join Date
    Jun 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Regular Expressions (regex) Greif

    Yeah, my input string was "Time IRIGB (s) Lat Ltn (deg) Lon Ltn (deg) Tas stbd (m/s) wind speed Hg (m/s) wind direction Hg (deg t)"

    and my program output was:
    Time IRIGB (s)
    Lat Ltn (deg)
    Lon Ltn (deg)
    Tas stbd (m/s)
    wind speed Hg (m/s)
    wind direction Hg (deg t)

    I had to change my regular expression, like the string in Pattern.compile().
    I changed it to "(\\w[\\w|\\s]+\\([\\w|\\s|\\/]*\\))" which means:
    - starts with a word character ( an alphanumeric )
    - then 1 or more alphanumerics or spaces
    - then a open parentheses (
    - then 0 or more alphanumerics or spaces or /'s
    - then a close parentheses )
    - the brackets around the entire expression means that thats what I want to pull out of the string with match.group() and match.find()

    Yeah, it took me like an hour to figure out that you need \\ cause you have to escape the slash in the string then escape your actual character class cause java's strings use \ as a special character grrr. But pretty much how you use java's regex is like this:

    - if you just want to split a string with a simple delimiter you can create a pattern via:
    REGEX = " ";
    Pattern p = Pattern.compile(REGEX);
    String[] outputs = p.split(yourInputString);

    this will split the string by spaces

    - but, if you want to split a string into things separated more complex then just spaces you have to do the way I did it, compiling an actual regular expression as opposed to just the delimiter used to split a string, except you'll now have to use the Matcher class and do matcher.find() to find the location of the first match and then matcher.group() ( matcher.group also can have int parameters which specify more complex regular expressions where you have matches within matches that you want to pull out ) then matcher.find() for the next one and another matcher.group() and so on, finally matcher.find() will return false when there is nothing more to search.
    Last edited by username9000; June 11th, 2009 at 05:56 PM.

Similar Threads

  1. Text Processing with Regular Expressions explained in Java
    By JavaPF in forum Java Programming Tutorials
    Replies: 3
    Last Post: February 8th, 2022, 05:16 PM
  2. Replies: 3
    Last Post: December 22nd, 2011, 09:46 AM
  3. Replies: 1
    Last Post: February 28th, 2009, 10:05 PM