Results 1 to 6 of 6
Thread: RegExPlus - Java regex library
- 11-14-2009, 03:04 AM #1
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.
- Named capture groups and back references
- Ability to reference a specific group
- even in the case where multiple groups have the same name - Perl's \gn and \g{n} syntax for back references and relative references
- If-Then-Else Conditionals
- "Branch reset" subpatterns
- Matching of a numeric range
- Option to ...
- VERIFY_GROUPS exist (when compiling a pattern)
- Use PERL_OCTAL syntax (instead of Java's)
- Use DOTNET_NUMBERING for capture groups (instead of Java's)
- .NET's EXPLICIT_CAPTURE option (?n)
- Added syntax ...
- POSIX classes via [:class:] syntax
- \x{hhh..} syntax for matching a character
- \X - match a single grapheme - similar to the dot, which matches (almost) any character
- (?#comment) syntax for embedded comments
- In Java 1.5, fixes bug involving the \Q...\E escape sequence.
- Natural sorting using the static naturalCompareTo method in the Pattern class.
- Supports Java 1.5+
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 08:31 PM.
CodesAway - codesaway.info
writing tools that make writing code a little easier
- 01-07-2010, 03:12 AM #2
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 03:17 AM.
- 01-07-2010, 04:50 AM #3
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 ")".
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.Java Code:^[COLOR="Red"](?<openPar>\Q(\E)?[/COLOR](?<areaCode>\d{3})[COLOR="#ff0000"](?('openPar')\Q)\E(?#closePar))[/COLOR]-(?<first3>\d{3})-(?<last4>\d{4})$
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.
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 08:49 PM.
CodesAway - codesaway.info
writing tools that make writing code a little easier
- 01-07-2010, 10:24 PM #4
oo nice. so how would u match the contents of a String obj using regex? Is it possible?
- 01-07-2010, 11:26 PM #5
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.
Note that the later case is the same as testing for equality (since, by default, the regex is case-sensitive).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()); } }
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
- 02-28-2010, 08:15 PM #6
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
-
help with designing Java program:file browser w/ regex search, possibly media player?
By jmd9qs in forum New To JavaReplies: 0Last Post: 11-04-2009, 09:09 PM -
java.lang.UnsatisfiedLinkError: no parport in java.library.path
By Heather in forum NetBeansReplies: 3Last Post: 09-07-2009, 01:28 PM -
How to add jar library in java?
By anilshelar in forum Advanced JavaReplies: 10Last Post: 09-02-2009, 04:31 AM -
Tell me jar file for library library org.bouncycastle.cms
By 82rathi.angara in forum New To JavaReplies: 10Last Post: 09-09-2008, 05:11 AM -
P~ 0.9 : Java-friendly scripting with powerful regex
By p7eregex in forum Java SoftwareReplies: 0Last Post: 12-17-2007, 08:10 PM


LinkBack URL
About LinkBacks


Bookmarks