Results 1 to 16 of 16
  1. #1
    shin_orochi is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Java RegEx problem, need help

    Hi all,

    i am trying to learning about regular expression in java. i have some problem, here my code :

    Java Code:
    import java.io.*;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    import java.io.Console;
    
    class RegularExpression {
      public static void main(String args[]) {
        try {
    		Console console = System.console();
    
    
             Pattern pattern = Pattern.compile("ing|les");
             Matcher matcher = pattern.matcher("About 2 months after being laid, the turtles hatchlings emerge from the sand and are put into sea water tanks");
    
    
    		 boolean found = false;
    		 while (matcher.find()) {
    			 console.format("I found the text" +
    				 " \"%s\" starting at " +
    				 "index %d and ending at index %d.%n",
    				 matcher.group(),
    				 matcher.start(),
    				 matcher.end());
    
    			 found = true;
    		 }
    		 if(!found){
    			 console.format("No match found.%n");
    		}
         } catch (Exception e) {
          System.err.println(e);
        }
      }
    }
    and the result are :

    I found the text "ing" starting at index 23 and ending at index 26.
    I found the text "les" starting at index 41 and ending at index 44.
    I found the text "ing" starting at index 51 and ending at index 54.

    my problem is, how to display the word "being" instead "I found the text "ing" starting at index 23 and ending at index 26." or "turtles" instead of "I found the text "les" starting at index 41 and ending at index 44.".


    Excuse for my English.
    Last edited by DarrylBurke; 03-09-2012 at 09:45 AM. Reason: Added code tags

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default Re: Java RegEx problem, need help

    Look around the FAQs section of this site and discover how to use code tags. I've added them for you this time.

    So, you want to capture all non-whitespace characters preceding "ing|or"? Or is it all non-punctuation|whitespace characters? or non-numeric characters?

    First formulate your requirements. Then try to illustrate them with examples. Examples without specs are meaningless.

    A couple of learning resources: Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns and Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    shin_orochi is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Re: Java RegEx problem, need help

    hi db,

    sorry for the mistake.

    yes, i want to capture the characters preceding "ing|or"

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java RegEx problem, need help

    Have a look at this Regular Expressions Tutorial. I think that the shorthand character classes, word boundaries and the lookahead sections are likely most appropriate for your problem.

  5. #5
    shin_orochi is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Re: Java RegEx problem, need help

    why when i try to put "ing$" as the regex, it returns "No math found"

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default Re: Java RegEx problem, need help

    What did you think the regex "ing$" signifies?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default Re: Java RegEx problem, need help

    Quote Originally Posted by shin_orochi View Post
    hi db,

    sorry for the mistake.

    yes, i want to capture the characters preceding "ing|or"
    You call that a specification? Whitespace characters are characters. Alpha characters are characters. Digit/numeric characters are characters. Punctuation characters are characters.

    So
    the characters preceding "ing|or"
    would mean from the beginning of input up to each "ing|or"

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    shin_orochi is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Re: Java RegEx problem, need help

    regex "ing$" will return any words ended with "ing", am i right?

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java RegEx problem, need help

    Quote Originally Posted by shin_orochi View Post
    regex "ing$" will return any words ended with "ing", am i right?
    Nope, the '$' indicates the end of the string. <-- this would match if there wasn't a period ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    shin_orochi is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Re: Java RegEx problem, need help

    ooo i see,

    then how to capture only the words ended with 'ing' ?

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default Re: Java RegEx problem, need help

    You've been given the links to two learning resources. One of the links has already been posted twoc.e

    Are we to believe that you went through the material and are still unable to come up with an approximation of what might go into the regex? I'd sooner believe that you glanced at the pages and decided it might be easier to hang around here till someone spoonfeeds you the answer.

    That's not going to happen. Not likely anyways.

    And your specs are still vague and incomplete.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    shin_orochi is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Re: Java RegEx problem, need help

    yaay!! finally i solve it.

    Thank you DB and JosAH.

    it's not like i'm not read all those articles, i need sometime to understand it.

    here is my code

    Java Code:
    import java.io.*;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    import java.util.*;
    
    class FileRead {
      public static void main(String args[]) {
        try {
    		int count = 0;
    		String txtFile;
    		String str;
    		Console console = System.console();
    		boolean found = false;
    
    		while (true) {
    			Pattern pattern = Pattern.compile(console.readLine("%nEnter your regex: "));
    			BufferedReader fileName = new BufferedReader(new InputStreamReader(System.in));
    		 	System.out.print("Please enter directory of the file: ");
    		 	System.out.flush();
    		 	txtFile = fileName.readLine();
    		 	FileInputStream fstream = new FileInputStream(txtFile);
    		 	DataInputStream in = new DataInputStream(fstream);
    			BufferedReader br = new BufferedReader(new InputStreamReader(in));
    
    			while((str = br.readLine()) !=null){
    				StringTokenizer token = new StringTokenizer(str);
    
    				while(token.hasMoreTokens()) {
    					str = token.nextToken();
    					Matcher matcher = pattern.matcher(str);
    
    					while (matcher.find()) {
    						count++;
    						System.out.println("i've found "+str);
    						found = true;
    					}
    				}
    			}
    
    			System.out.println("Total word/s found= "+count);
    			count = 0;
    			if(!found) {
    				System.out.println("No match found");
    			}
    		}
    	}
    	catch (Exception e) {
    		System.err.println(e);
        }
      }
    }
    Thanks for the help, i really appreciate it.

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default Re: Java RegEx problem, need help

    Quote Originally Posted by shin_orochi View Post
    i need sometime to understand it.
    Very true.

    here is my code
    I don't see any regex in that code.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java RegEx problem, need help

    Quote Originally Posted by DarrylBurke View Post
    I don't see any regex in that code.
    The user has to enter one during runtime.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default Re: Java RegEx problem, need help

    Quote Originally Posted by JosAH View Post
    The user has to enter one during runtime.
    I saw that, but how does it relate to the question asked in this thread?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java RegEx problem, need help

    Quote Originally Posted by DarrylBurke View Post
    I saw that, but how does it relate to the question asked in this thread?
    It was also about regular expressions?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. regex problem
    By fishy8158 in forum New To Java
    Replies: 14
    Last Post: 11-24-2011, 03:59 PM
  2. regex problem
    By javaPower in forum Advanced Java
    Replies: 7
    Last Post: 10-30-2011, 07:15 AM
  3. How to use regex and split to solve a problem
    By PeteClimbs in forum New To Java
    Replies: 2
    Last Post: 04-19-2011, 08:31 PM
  4. breaking up a string, a regex problem!!
    By A.n.H in forum Advanced Java
    Replies: 0
    Last Post: 05-17-2010, 03:03 PM
  5. Regex problem
    By Nimyz in forum Advanced Java
    Replies: 4
    Last Post: 05-14-2010, 07:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •