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;
}
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\\_一-龥]+\\}"));
}
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;
}
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;
}
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 + "]");
}
}
Aggregations