Results 1 to 6 of 6

Thread: Problem with Regex in returning string

  1. #1
    Join Date
    Dec 2009
    Posts
    204

    Problem with Regex in returning string

    Hello,
    I have a little problem with regular expressions. I try to return a string that would be of this form here: <123456789@sample.com>. But I can not return string with the regex. Here is the code for it.
    Code:
    Public class TestRegexp {
    
    	Public static void hand(String[] args) {
    
    Pattern pat;
    Matcher mth;
    String psenc;
    
    pat = Pattern.compiles("^<*>$");
    = pat mth.mth("+ OK hello <12356@monentreprise.com>");
    psenc = mth.group();
    System.out.System.out.println(psenc);
    	}
     
    }
    Does someone could tell me what is wrong with the code ? because normally if I understand the character represents the beginning of string, so I search out the "first, then the star represents any number of character and I close it.

  2. #2
    Join Date
    Nov 2009
    Posts
    518

    Re: Problem with Regex in returning string

    Hello,
    I think there are many problems here. First group () used to obtain a search result, or you have defined the match but you did not initiate research on it. You must use either match () to verify the completeness of your string matches the regular expression, or find () where fragments of the string that match. If it returns true you is that the match / search is successful and you can use group. If the term was not found, group has no meaning and fails. Then, '*' means that the character or group previous can be repeated 0 to infinity. If you want any character is repeated to infinity, we must use '.*' car '. " means any character. Moreover '^ <' means that it is imperative that the first character after the start is a '<', but this is obviously not what you want. Similarly '> $' indicates that the line should absolutely end with a '>'.

  3. #3
    Join Date
    Dec 2009
    Posts
    204

    Re: Problem with Regex in returning string

    Hello,
    Thanks for that, here is my modified code, if you can just go through it and if there are any mistakes then please guide me with the same.
    Code:
    Public class TestRegexp {
     
    	Public static void hand(String[] args) {
    
    Pattern pat;
    Matcher mth;
    		boolean foo;
    
    pat = Pattern.compiles("<.*>");
    = pat mth.mth("+ OK hello <123567@monentreprise.com>");
    foo = mth.find();
    		if(toto)
    		System.out.System.out.println(mth.group());
    	}
     
    }

  4. #4
    Join Date
    Nov 2009
    Posts
    356

    Re: Problem with Regex in returning string

    Hello,
    If you want to retrieve all of your regular expression, because it does not serve you because group () without parameter corresponds group (0) which contain any expression. For story, you may want to recover parts of speech. You could for example be used as regular expression "<(.*)@(.*)>".
    Thus:
    - Group (1) return "123,567"
    - Group (2) return "monentreprise.com"

  5. #5
    Join Date
    Nov 2009
    Posts
    333

    Re: Problem with Regex in returning string

    Hello,
    I think you should vary of "*" is said greedy, it consumes as many characters as possible until the regexp is valid. Rather "*?" is reluctant says, it consumes the minimum possible to make valid string.
    For example the string "<un> zzzzzzz zzzzz zzzzzz <deux>
    - "<.*>" Find the expression "<un> zzzzz <deux>"
    - "<.*?>" Find the expression "<un>" first find () then "<deux>" second find ()

  6. #6
    Join Date
    Dec 2009
    Posts
    204

    Re: Problem with Regex in returning string

    Hello,
    Ok, thank you for the clarification (I still think that you had to make a mistake in your code because it is 2 times the same thing). By cons I tried your example, by either: <.?> Which returns me an error and I also tried <*?> Which returns me the last character. If you have any more ideas about then please let me know. Or is there an alternative solution for this?

Similar Threads

  1. Problem in retrieving string with Regex
    By Aaliya Seth in forum Software Development
    Replies: 5
    Last Post: 11-02-2010, 05:53 AM
  2. Regex - compose a string
    By Aaliya Seth in forum Software Development
    Replies: 5
    Last Post: 09-02-2010, 01:38 AM
  3. Regex string with quotes
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 07-02-2010, 06:03 AM
  4. Regex replace an integer with string
    By New ID in forum Software Development
    Replies: 5
    Last Post: 05-02-2010, 01:35 AM
  5. Regex strip out all non alpha Characters from a String
    By Chhaya in forum Software Development
    Replies: 2
    Last Post: 22-05-2009, 06:51 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,482,863.72656 seconds with 17 queries