Results 1 to 6 of 6
  1. #1
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default RegExPlus - Java regex library (now free)

    RegExPlus
    RegExPlus extends Java's regular expression syntax by adding support for additional Perl and .NET syntax.

    These additions make it possible to use the same regular expression in Java that you already use in other RegEx engines.

    Features
    100% backwards compatible with Java regular expressions plus the below additional syntax.

    Also available: JavaStar - online regular expression tester that uses RegExPlus to check for matches.


    Edit:
    Sorry for the long wait, RegExPlus is finally free to download for both personal and commercial use.


    For the record, don't fall prey to peer pressure. When friends tell you to sell something that you want to give away for free, don't listen to your friends. Again, sorry for the long wait, enjoy the library.

    Check out the forums to Ask for Help, file a Bug Report, make Feature Requests, or to check out and share Tips and Tricks.
    Last edited by CodesAway; 02-28-2010 at 09:31 PM.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  2. #2
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    oo if statements sound pretty cool. got a question on the \Q .. \E though. would this match?

    test.matches("\Q test \E") - would that match the String test to itself? I realize it's incongruous but would it work? If not, how are we suppose to use \Q ... \E ?
    Last edited by Lil_Aziz1; 01-07-2010 at 04:17 AM.

  3. #3
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Quote Originally Posted by Lil_Aziz1 View Post
    oo if statements sound pretty cool.
    They are. I find them useful for tailing specific needs, when you want to match only specific formats.

    For example, let's say I want to match a US phone number. For a simple case, let's allow matches only in the form "(123)-456-7890" or "123-456-7890". You can use an if statement to ensure that if it starts with "(", it has the corresponding ")".

    Java Code:
    ^[COLOR="Red"](?<openPar>\Q(\E)?[/COLOR](?<areaCode>\d{3})[COLOR="#ff0000"](?('openPar')\Q)\E(?#closePar))[/COLOR]-(?<first3>\d{3})-(?<last4>\d{4})$
    In JavaStar you can see that this regex does match, for example, (123)-456-7890 and 123-456-7890, but not (123-456-7890 or 123)-456-7890.

    Of course, there are other regexes that will work, but this one will capture the three parts into named groups. If you didn't use conditionals, the only other way I can think of would be to use duplicate named groups.


    Quote Originally Posted by Lil_Aziz1 View Post
    got a question on the \Q .. \E though. would this match?

    test.matches("\Q test \E") - would that match the String test to itself? I realize it's incongruous but would it work? If not, how are we suppose to use \Q ... \E ?
    It matches " test " (with a leading a trailing space), since there is a leading a trailing space in your example.

    Now, if you are asking if it matches the contents of <test>, then no (unless test.equals(" test ")).


    The \Q ... \E block is meant to allow matching a string literal, which otherwise might be interpreted as meta-characters. For example, the regex, "(", would create an error, since "(" is a meta-character (used to start a group). To match a literal "(", you can put it inside a "\Q ... \E" block - "\Q(\E" will match "(" - see the results in JavaStar. You could also have preceded the "(" with a "\", which will quote the next character, "\(".


    Note that surrounding a string with "\Q ... \E", in general, will not match the literal string. To do this, use pattern.quote(String s).

    The latter will ALWAYS return a regex that matches the passed string literal. For example, to match the string "\E" literally, the regex "\Q\E\E" will not work. In this regex, the first "\E" will close the quotation group, and the second "\E" will cause an error - see the results in JavaStar.

    If you use the quote method, the returned regex is "\Q\E\\E\Q\E", which does match "\E" (JavaStar result).
    Last edited by CodesAway; 01-07-2010 at 09:49 PM.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  4. #4
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    oo nice. so how would u match the contents of a String obj using regex? Is it possible?

  5. #5
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Quote Originally Posted by Lil_Aziz1 View Post
    oo nice. so how would u match the contents of a String obj using regex? Is it possible?
    Yes it is, use Pattern.quote(String s), with "s" being the string you want to match. This will return a regex which will match the contents of "s".

    Java Code:
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Test
    {
    	/**
    	 * Uses a regex to match a String's contents.
    	 * 
    	 * @param args
    	 *            (not used)
    	 */
    	public static void main(String[] args)
    	{
    		String input = "This can be ANY string.";
    
    		// returns a regex which will match the given input as a literal
    		// (i.e. meta-characters are given no special meaning)
    		String regex = Pattern.quote(input);
    
    		Pattern pattern = Pattern.compile(regex);
    		Matcher matcher = pattern.matcher(input);
    		System.out.println("Matches: " + matcher.matches());
    	}
    }

    Or, you can use the LITERAL flag when compiling the pattern.

    Java Code:
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    import static java.util.regex.Pattern.LITERAL;
    
    public class Test
    {
    	/**
    	 * Uses a regex to match a String's contents.
    	 * 
    	 * @param args
    	 *            (not used)
    	 */
    	public static void main(String[] args)
    	{
    		String input = "This can be ANY string.";
    
    		Pattern pattern = Pattern.compile(input, LITERAL);
    		Matcher matcher = pattern.matcher(input);
    		System.out.println("Matches: " + matcher.matches());
    	}
    }
    Note that the later case is the same as testing for equality (since, by default, the regex is case-sensitive).

    The first case only treats the quoted part as a literal, which allows for using it as part of a larger regex. The second case treats the ENTIRE pattern as a literal.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  6. #6
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Sorry for the long wait, RegExPlus is finally free to download for both personal and commercial use.


    For the record, don't fall prey to peer pressure. When friends tell you to sell something that you want to give away for free, don't listen to your friends. Again, sorry for the long wait, enjoy the library.

    Check out the forums to Ask for Help, file a Bug Report, make Feature Requests, or to check out and share Tips and Tricks.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2009, 10:09 PM
  2. Replies: 3
    Last Post: 09-07-2009, 02:28 PM
  3. How to add jar library in java?
    By anilshelar in forum Advanced Java
    Replies: 10
    Last Post: 09-02-2009, 05:31 AM
  4. Tell me jar file for library library org.bouncycastle.cms
    By 82rathi.angara in forum New To Java
    Replies: 10
    Last Post: 09-09-2008, 06:11 AM
  5. P~ 0.9 : Java-friendly scripting with powerful regex
    By p7eregex in forum Java Software
    Replies: 0
    Last Post: 12-17-2007, 09:10 PM

Posting Permissions

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