Skip to content

add symbols map for pattern character #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 19 additions & 51 deletions src/main/java/ru/lanwen/verbalregex/VerbalExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import static java.lang.String.valueOf;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -18,6 +20,16 @@ public static class Builder {
private StringBuilder suffixes = new StringBuilder();
private int modifiers = Pattern.MULTILINE;

private static final Map<Character, Integer> SYMBOL_MAP = new HashMap<Character, Integer>() {{
put('d', Pattern.UNIX_LINES);
put('i', Pattern.CASE_INSENSITIVE);
put('x', Pattern.COMMENTS);
put('m', Pattern.MULTILINE);
put('s', Pattern.DOTALL);
put('u', Pattern.UNICODE_CASE);
put('U', Pattern.UNICODE_CHARACTER_CLASS);
}};

/**
* Package private. Use {@link #regex()} to build a new one
*
Expand Down Expand Up @@ -172,7 +184,7 @@ public Builder find(final String value) {
public Builder maybe(final String pValue) {
return this.then(pValue).add("?");
}

/**
* Add a regex to the expression that might appear once (or not)
* Example:
Expand Down Expand Up @@ -367,60 +379,16 @@ public Builder range(final String... pArgs) {
}

public Builder addModifier(final char pModifier) {
switch (pModifier) {
case 'd':
modifiers |= Pattern.UNIX_LINES;
break;
case 'i':
modifiers |= Pattern.CASE_INSENSITIVE;
break;
case 'x':
modifiers |= Pattern.COMMENTS;
break;
case 'm':
modifiers |= Pattern.MULTILINE;
break;
case 's':
modifiers |= Pattern.DOTALL;
break;
case 'u':
modifiers |= Pattern.UNICODE_CASE;
break;
case 'U':
modifiers |= Pattern.UNICODE_CHARACTER_CLASS;
break;
default:
break;
if (SYMBOL_MAP.containsKey(pModifier)) {
modifiers |= SYMBOL_MAP.get(pModifier);
}

return this;
}

public Builder removeModifier(final char pModifier) {
switch (pModifier) {
case 'd':
modifiers &= ~Pattern.UNIX_LINES;
break;
case 'i':
modifiers &= ~Pattern.CASE_INSENSITIVE;
break;
case 'x':
modifiers &= ~Pattern.COMMENTS;
break;
case 'm':
modifiers &= ~Pattern.MULTILINE;
break;
case 's':
modifiers &= ~Pattern.DOTALL;
break;
case 'u':
modifiers &= ~Pattern.UNICODE_CASE;
break;
case 'U':
modifiers &= ~Pattern.UNICODE_CHARACTER_CLASS;
break;
default:
break;
if (SYMBOL_MAP.containsKey(pModifier)) {
modifiers &= ~SYMBOL_MAP.get(pModifier);
}

return this;
Expand Down Expand Up @@ -578,7 +546,7 @@ public Builder or(final String pValue) {
}
return this;
}

/**
* Adds an alternative expression to be matched
* based on an array of values
Expand Down Expand Up @@ -750,7 +718,7 @@ public String getText(final String toTest, final int group) {

/**
* Extract exact group from string and add it to list
*
*
* Example:
* String text = "SampleHelloWorldString";
* VerbalExpression regex = regex().capt().oneOf("Hello", "World").endCapt().maybe("String").build();
Expand Down