Search in sources :

Example 11 with Pattern

use of java.util.regex.Pattern in project eweb4j-framework by laiweiwei.

the class Tags method findByRegex.

public static List<String> findByRegex(String input, String regex) {
    if (input == null || input.trim().length() == 0)
        return null;
    List<String> result = new ArrayList<String>();
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(input);
    while (m.find()) {
        result.add(m.group());
    }
    if (result.isEmpty())
        return null;
    return result;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList)

Example 12 with Pattern

use of java.util.regex.Pattern in project eweb4j-framework by laiweiwei.

the class RegexList method main.

public static void main(String[] args) throws UnsupportedEncodingException {
    Pattern pattern = Pattern.compile(RegexList.has_chinese_regexp);
    String str = "xxxx微微=微微=是";
    Matcher matcher = pattern.matcher(str);
    while (matcher.find()) {
        String g = matcher.group();
        str = str.replace(g, URLEncoder.encode(g, "utf-8"));
        System.out.println(g);
    }
    System.out.println(str);
//System.out.println("/users/{name}".matches("\\{[0-9a-zA-Z\\_一-龥]+\\}"));
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher)

Example 13 with Pattern

use of java.util.regex.Pattern in project eweb4j-framework by laiweiwei.

the class CommonUtil method replaceChinese2Utf8.

public static String replaceChinese2Utf8(String source) {
    String str = source;
    try {
        Pattern pattern = Pattern.compile(RegexList.has_chinese_regexp);
        Matcher matcher = pattern.matcher(source);
        while (matcher.find()) {
            String g = matcher.group();
            str = source.replace(g, URLEncoder.encode(g, "utf-8"));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return str;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ParseException(java.text.ParseException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 14 with Pattern

use of java.util.regex.Pattern in project eweb4j-framework by laiweiwei.

the class CommonUtil method parsePropValue.

public static String parsePropValue(String source, String _propId) {
    if (_propId != null)
        return parseSinglePropVarable(source, _propId);
    Pattern pattern = Pattern.compile(RegexList.property_regexp);
    Matcher matcher = pattern.matcher(source);
    if (!matcher.find())
        return source;
    String g = matcher.group();
    String suffix = source.replace(g, "");
    String[] props = g.replace("${", "").replace("}", "").split("\\.");
    String prefix = null;
    if (props.length == 2) {
        String propId = props[0];
        String key = props[1];
        if ("global".equals(propId)) {
            prefix = Props.get(key);
        } else {
            prefix = Props.getMap(propId).get(key);
        }
        source = prefix + suffix;
    }
    return source;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher)

Example 15 with Pattern

use of java.util.regex.Pattern in project languagetool by languagetool-org.

the class UselessExampleFinder method removeLinesFromXml.

// Note: this is a bad hack, we just iterate through the file's lines
private void removeLinesFromXml(Rule rule, String sentenceToRemove, List<String> xmlLines) {
    List<Integer> linesToRemove = new ArrayList<>();
    String currentRuleId = null;
    Pattern pattern = Pattern.compile(".*id=[\"'](.*?)[\"'].*");
    String expectedSubId = ((AbstractPatternRule) rule).getSubId();
    int lineCount = 0;
    int subRuleCount = 0;
    int removedCount = 0;
    boolean inRuleGroup = false;
    for (String xmlLine : xmlLines) {
        if (xmlLine.contains("<rulegroup")) {
            subRuleCount = 0;
            inRuleGroup = true;
        } else if (xmlLine.contains("</rulegroup>")) {
            subRuleCount = 0;
            inRuleGroup = false;
        } else if ((xmlLine.contains("<rule ") || xmlLine.contains("<rule>")) && inRuleGroup) {
            subRuleCount++;
        }
        Matcher m = pattern.matcher(xmlLine);
        if (m.matches()) {
            currentRuleId = m.group(1);
        }
        if (!xmlLine.contains("correction=") && xmlLine.contains(sentenceToRemove + "</example>")) {
            if (currentRuleId != null && !currentRuleId.equals(rule.getId())) {
                lineCount++;
                continue;
            }
            if (!inRuleGroup) {
                subRuleCount = 1;
            }
            if (!expectedSubId.equals("0") && !expectedSubId.equals(String.valueOf(subRuleCount))) {
                lineCount++;
                continue;
            }
            linesToRemove.add(lineCount);
            break;
        }
        lineCount++;
    }
    // start from end, as we need to remove items
    Collections.reverse(linesToRemove);
    for (Integer s : linesToRemove) {
        xmlLines.remove(s.intValue());
        removedLinesCount++;
        removedCount++;
    }
    if (removedCount == 0) {
        System.err.println("No line removed: " + rule + "[" + expectedSubId + "]");
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule)

Aggregations

Pattern (java.util.regex.Pattern)3181 Matcher (java.util.regex.Matcher)2116 ArrayList (java.util.ArrayList)387 IOException (java.io.IOException)247 Test (org.junit.Test)238 File (java.io.File)193 HashMap (java.util.HashMap)163 BufferedReader (java.io.BufferedReader)127 Field (java.lang.reflect.Field)119 PatternSyntaxException (java.util.regex.PatternSyntaxException)119 Map (java.util.Map)110 List (java.util.List)93 HashSet (java.util.HashSet)79 InputStreamReader (java.io.InputStreamReader)67 InputStream (java.io.InputStream)43 FileReader (java.io.FileReader)41 FileInputStream (java.io.FileInputStream)40 URL (java.net.URL)35 SmallTest (android.test.suitebuilder.annotation.SmallTest)31 LinkedHashMap (java.util.LinkedHashMap)31