MentaRegex

Compare Revisions

Ignore whitespace Rev 19 → Rev 20

/tags/menta-regex-0.9.8/src/test/java/org/mentaregex/RegexTest.java
New file
0,0 → 1,128
package org.mentaregex;
 
import static org.junit.Assert.*;
import static org.mentaregex.Regex.*;
 
import org.junit.Test;
 
public class RegexTest {
 
@Test
public void testMatches() {
 
final String s = "sergio_oliveira_jr";
 
assertTrue(matches(s, "/vei/"));
assertTrue(matches(s, "/_jr$/"));
assertTrue(matches(s, "/_jr/"));
assertTrue(matches(s, "/^sergio/"));
assertTrue(matches(s, "/sergio/"));
 
assertTrue(matches(s, "/^s.+r$/"));
assertTrue(matches(s, "/s.+r$/"));
assertTrue(matches(s, "/^s.+r/"));
assertTrue(matches(s, "/s.+r/"));
 
assertTrue(matches(s, "/_\\w+_/"));
 
assertFalse(matches(s, "/\\d/"));
assertFalse(matches(s, "/^oliveira/"));
assertFalse(matches(s, "/oliveira$/"));
}
 
@Test
public void testMatchesIgnoreCase() {
 
final String s = "sergio_OlIvEIrA_jr";
 
assertTrue(matches(s, "/oliveira/i"));
assertFalse(matches(s, "/oliveira/"));
}
 
private static void check(final String[] matches, final String... s) {
 
assertEquals(matches.length, s.length);
 
for (int i = 0; i < matches.length; i++) {
assertEquals(matches[i], s[i]);
}
}
 
@Test
public void testMatch() {
 
String[] matches;
 
final String s = "aa11bb22cc33";
 
matches = match(s, "/aa11(\\w\\w)/");
 
check(matches, "bb");
 
matches = match(s, "/(\\d\\d)/");
 
check(matches, "11");
 
matches = match(s, "/(AA)/");
 
assertNull(matches);
 
matches = match(s, "/(AA)/i");
 
check(matches, "aa");
 
matches = match(s, "/11([a-z]+\\d+)/");
 
check(matches, "bb22");
}
 
@Test
public void testMatchGlobal() {
 
String[] matches;
 
final String s = "aa11bb22cc33";
 
matches = match(s, "/(\\d\\d)/g");
 
check(matches, "11", "22", "33");
 
matches = match(s, "/([a-z]{2})/g");
 
check(matches, "aa", "bb", "cc");
 
matches = match(s, "/[a-z]{2}(\\d{2})/g");
 
check(matches, "11", "22", "33");
}
@Test
public void testSpecialCase() {
String s = "aaaaaa [tag]house[/tag] bbbbb [tag]home[/tag] cccccccccccc";
String[] m1 = Regex.match(s, "/\\[tag\\](.*?)\\[\\/tag\\]/");
assertEquals("house", m1[0]);
}
 
@Test
public void testSubstitution() {
 
assertEquals("sergioAApelaAA", sub("sergio33pela44", "s/\\d+/AA/gi"));
 
assertEquals("sergiopelaAAA", sub("sergioAAApelaAAA", "s/AAA//"));
 
assertEquals("sergiopela", sub("sergioAAApelaAAA", "s/AAA//g"));
 
assertEquals("sergioCCCpelaCCC", sub("sergioCCCpelaCCC", "s/c//"));
 
assertEquals("sergioCCpelaCCC", sub("sergioCCCpelaCCC", "s/c//i"));
 
assertEquals("sergiopela", sub("sergioCCCpelaCCC", "s/c//gi"));
 
assertEquals("sergio/Z/Zpela", sub("sergioAApela", "s/AA/\\/Z\\/Z/g"));
 
assertEquals("sergio\\Z\\Zpela", sub("sergioAApela", "s/AA/\\\\Z\\\\Z/g"));
}
}
/tags/menta-regex-0.9.8/src/main/java/org/mentaregex/Regex.java
New file
0,0 → 1,398
package org.mentaregex;
 
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
* A perl-like regex support for Java.
*
* @author Sergio Oliveira Jr. (sergio.oliveirajr@gmail.com)
*/
public class Regex {
 
private static final String TOKEN1 = "A1cxxZ";
 
private static final String TOKEN2 = "Bbsd423xx12asdf44xT";
 
private static final char DEFAULT_ESCAPE_CHAR = '\\';
 
private final String s;
 
private final String escPattern;
 
private final boolean caseInsensitive;
 
private final boolean global;
 
private final boolean substitute;
 
private final String toSub;
 
/**
* Creates a new regex expression.
*
* @param s The string to be matched or substituted
* @param pattern The regex pattern
* @param escape The escape char to use
*/
public Regex(final String s, String pattern, final char escape) {
 
this.s = s;
 
this.substitute = pattern.startsWith("s/");
 
this.global = pattern.endsWith("/gi") || pattern.endsWith("/ig") || pattern.endsWith("/g");
 
this.caseInsensitive = pattern.endsWith("/gi") || pattern.endsWith("/ig") || pattern.endsWith("/i");
 
pattern = removeSlashes(pattern);
 
if (!substitute) {
 
if (escape != DEFAULT_ESCAPE_CHAR) {
 
this.escPattern = changeEscapeChar(pattern, escape);
 
}
else {
 
this.escPattern = pattern;
}
 
this.toSub = null;
 
}
else {
 
String token = null;
 
if (!pattern.contains(TOKEN1)) {
 
token = TOKEN1;
 
}
else if (!pattern.contains(TOKEN2)) {
 
token = TOKEN2;
 
}
else {
 
throw new RuntimeException("Cannot use mentawai Regex with this pattern!");
}
 
// this is to allow '/' both in pattern and sub expression...
// Necessary so that split returns only 2 items...
// '/' must be escaped!
final String newPattern = pattern.replaceAll("\\" + escape + "/", token);
 
boolean changed = false;
 
if (!newPattern.equals(pattern)) {
 
pattern = newPattern;
 
changed = true;
}
 
String[] parts = pattern.split("/");
 
if (parts.length == 1 && pattern.endsWith("/")) {
 
final String save = parts[0];
 
parts = new String[2];
parts[0] = save;
parts[1] = "";
 
}
else if (parts.length != 2) {
 
throw new IllegalArgumentException("Bad substitute pattern: " + pattern);
}
 
if (changed) {
 
this.toSub = parts[1].replaceAll(token, "/");
 
}
else {
 
this.toSub = parts[1];
}
 
if (escape != DEFAULT_ESCAPE_CHAR) {
 
if (changed) {
 
this.escPattern = changeEscapeChar(parts[0], escape).replaceAll(token, "\\/");
 
}
else {
 
this.escPattern = changeEscapeChar(parts[0], escape);
 
}
 
}
else {
 
if (changed) {
 
this.escPattern = parts[0].replaceAll(token, "\\/");
 
}
else {
 
this.escPattern = parts[0];
 
}
}
}
 
}
 
/**
* Creates a new regex expression.
*
* @param s The string to be matched or substituted
* @param pattern The regex pattern
*/
public Regex(final String s, final String pattern) {
 
this(s, pattern, DEFAULT_ESCAPE_CHAR);
 
}
 
private static String removeSlashes(String s) {
 
if (s.startsWith("s/")) {
 
s = s.substring(2, s.length());
 
}
else if (s.startsWith("/")) {
 
s = s.substring(1, s.length());
}
 
if (s.endsWith("/gi") || s.endsWith("/ig")) {
 
s = s.substring(0, s.length() - 3);
 
}
else if (s.endsWith("/g") || s.endsWith("/i")) {
 
s = s.substring(0, s.length() - 2);
 
}
else if (s.endsWith("/")) {
 
s = s.substring(0, s.length() - 1);
}
 
return s;
}
 
private static String changeEscapeChar(final String s, final char esc) {
 
return s.replace(esc, '\\');
}
 
/**
* Substitute and return the new string.
*
* @return The new string, in other words, the result of this regex substitution
*/
public String substitute() {
 
Pattern p = null;
 
if (caseInsensitive) {
 
p = Pattern.compile(escPattern, Pattern.CASE_INSENSITIVE);
 
}
else {
 
p = Pattern.compile(escPattern);
}
 
final Matcher m = p.matcher(s);
 
if (global) {
 
return m.replaceAll(toSub);
 
}
else {
 
return m.replaceFirst(toSub);
}
 
}
 
/**
* Tell whether there is a match for this regex.
*
* @return true if we have a match
*/
public boolean matches() {
 
Pattern p = null;
 
if (caseInsensitive) {
 
p = Pattern.compile(escPattern, Pattern.CASE_INSENSITIVE);
 
}
else {
 
p = Pattern.compile(escPattern);
}
 
final Matcher m = p.matcher(s);
 
return m.find();
}
 
/**
* Match and return what was matched. That's for when you use regular expressions with parenthesis.
*
* @return What was matched or an empty array if nothing was matched
*/
public String[] match() {
 
Pattern p = null;
 
if (caseInsensitive) {
 
p = Pattern.compile(escPattern, Pattern.CASE_INSENSITIVE);
 
}
else {
 
p = Pattern.compile(escPattern);
}
 
final Matcher m = p.matcher(s);
 
final List<String> res = new LinkedList<String>();
 
while (m.find()) {
 
final int x = m.groupCount();
 
for (int i = 0; i < x; i++) {
 
res.add(m.group(i + 1));
}
 
if (!global) {
break; // just once...
}
}
 
if (res.size() == 0) {
return null;
}
 
return res.toArray(new String[res.size()]);
 
}
 
/**
* Fast way to match.
*
* @param s The string to match
* @param pattern The regex pattern to match
* @return What was matched, if anything.
*/
public static String[] match(final String s, final String pattern) {
 
return match(s, pattern, DEFAULT_ESCAPE_CHAR);
}
 
/**
* Fast way to match.
*
* @param s The string to match
* @param pattern The regex pattern to match
* @return true if it was matched
*/
public static boolean matches(final String s, final String pattern) {
 
return matches(s, pattern, DEFAULT_ESCAPE_CHAR);
}
 
/**
* Fast way to match.
*
* @param s The string to match
* @param pattern The regex pattern to match
* @param escapeChar the escape char to use
* @return What was matched, if anything.
*/
public static String[] match(final String s, final String pattern, final char escapeChar) {
 
final Regex r = new Regex(s, pattern, escapeChar);
 
return r.match();
}
 
/**
* Fast way to match.
*
* @param s The string to match
* @param pattern The regex pattern to match
* @param escapeChar the escape char to use
* @return true if it was matched
*/
public static boolean matches(final String s, final String pattern, final char escapeChar) {
 
final Regex r = new Regex(s, pattern, escapeChar);
 
return r.matches();
}
 
/**
* Fast way to substitute.
*
* @param s The string to substitute
* @param pattern the pattern to match
* @return the new string
*/
public static String sub(final String s, final String pattern) {
 
return sub(s, pattern, DEFAULT_ESCAPE_CHAR);
}
 
/**
* Fast way to substitute.
*
* @param s The string to substitute
* @param pattern the pattern to match
* @param escapeChar the scape char to use
* @return the new string
*/
public static String sub(final String s, final String pattern, final char escapeChar) {
 
final Regex r = new Regex(s, pattern, escapeChar);
 
return r.substitute();
}
 
/**
* Escape a forward slash with the escape char provided. Great for directories for example.
*
* @param s The string to change
* @param escapeChar the escape char to use
* @return the new string with the slashed escaped with the given escape char
*/
public static String escapeSlash(final String s, final String escapeChar) {
 
return s.replaceAll("/", escapeChar + "/");
}
}
/tags/menta-regex-0.9.8/pom.xml
New file
0,0 → 1,401
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>oss-parent</artifactId>
<groupId>org.sonatype.oss</groupId>
<version>3</version>
</parent>
<groupId>me.soliveirajr</groupId>
<artifactId>menta-regex</artifactId>
<version>0.9.8</version>
<name>MentaRegex</name>
<description>Perl-like regular expressions support for Java.</description>
<url>http://maven.apache.org</url>
<licenses>
<license>
<name>GNU Lesser General Public License (LGPL), Version 2.1</name>
<url>http://www.fsf.org/licensing/licenses/lgpl.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<scm>
<connection>scm:svn:svn://soliveirajr.com/mentaregex/tags/menta-regex-0.9.8</connection>
<developerConnection>scm:svn:svn://soliveirajr.com/mentaregex/tags/menta-regex-0.9.8</developerConnection>
<url>svn://soliveirajr.com/mentaregex/tags/menta-regex-0.9.8</url>
</scm>
<distributionManagement>
<repository>
<id>sonatype-nexus-staging</id>
<name>Nexus Release Repository</name>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
<snapshotRepository>
<id>sonatype-nexus-snapshots</id>
<name>Sonatype Nexus Snapshots</name>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
<properties>
<build.final.name>mentaregex</build.final.name>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<svn.tags>svn://soliveirajr.com/mentaregex/tags</svn.tags>
<svn.url>svn://soliveirajr.com/mentaregex/trunk</svn.url>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>sonatype-nexus-snapshots</id>
<name>Sonatype Nexus Snapshots</name>
<url>http://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Maven Repository Switchboard</name>
<url>http://repo1.maven.org/maven2</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Maven Plugin Repository</name>
<url>http://repo1.maven.org/maven2</url>
</pluginRepository>
</pluginRepositories>
<build>
<finalName>mentaregex</finalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.0</version>
<configuration>
<mavenExecutorId>forked-path</mavenExecutorId>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<executions>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</execution>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</execution>
</executions>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.0</version>
<configuration>
<tagBase>svn://soliveirajr.com/mentaregex/tags</tagBase>
<connectionUrl>scm:svn:svn://soliveirajr.com/mentaregex/trunk</connectionUrl>
<mavenExecutorId>forked-path</mavenExecutorId>
<goals>install</goals>
</configuration>
</plugin>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<id>default-clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>default-install</id>
<phase>install</phase>
<goals>
<goal>install</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>default-resources</id>
<phase>process-resources</phase>
<goals>
<goal>resources</goal>
</goals>
</execution>
<execution>
<id>default-testResources</id>
<phase>process-test-resources</phase>
<goals>
<goal>testResources</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7.2</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>default-deploy</id>
<phase>deploy</phase>
<goals>
<goal>install</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>2.0.1</version>
<executions>
<execution>
<id>default-site</id>
<phase>site</phase>
<goals>
<goal>site</goal>
</goals>
<configuration>
<reportPlugins>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
</reportPlugin>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.5</version>
</reportPlugin>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8</version>
<configuration>...</configuration>
</reportPlugin>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
</reportPlugin>
</reportPlugins>
</configuration>
</execution>
<execution>
<id>default-deploy</id>
<phase>site-deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
<configuration>
<reportPlugins>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
</reportPlugin>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.5</version>
</reportPlugin>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8</version>
<configuration>...</configuration>
</reportPlugin>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
</reportPlugin>
</reportPlugins>
</configuration>
</execution>
</executions>
<configuration>
<reportPlugins>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
</reportPlugin>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.5</version>
</reportPlugin>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8</version>
<configuration>...</configuration>
</reportPlugin>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
</reportPlugin>
</reportPlugins>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
</plugin>
<plugin>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.5</version>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8</version>
</plugin>
</plugins>
</reporting>
<profiles>
<profile>
<id>release-sign-artifacts</id>
<activation>
<property>
<name>performRelease</name>
<value>true</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-gpg-plugin</artifactId>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
 
/tags/menta-regex-0.9.8/README.txt
New file
0,0 → 1,2
MentaRegex - The PERL-like regular expressions for Java.
 
/tags/menta-regex-0.9.8
Property changes:
Added: svn:ignore
+ target
.settings
.classpath
.project