use of java.util.regex.MatchResult in project openchemlib by Actelion.
the class StringFunctions method extractInverse.
/**
* @param str
* @param regex
* @return the combined not matching parts of the string.
*/
public static String extractInverse(String str, String regex) {
String substring = "";
Pattern pa = Pattern.compile(regex);
Matcher ma = pa.matcher(str);
if (ma.find()) {
MatchResult mr = ma.toMatchResult();
int start = mr.start();
int end = mr.end();
String rest1 = str.substring(0, start);
String rest2 = str.substring(end);
substring = rest1 + rest2;
}
return substring;
}
use of java.util.regex.MatchResult in project openchemlib by Actelion.
the class StringFunctions method formatToCharactersAndDigits.
public static String formatToCharactersAndDigits(String str) {
String regex = "[0-9a-zA-Z ]";
Pattern pa = Pattern.compile(regex);
Matcher ma = pa.matcher(str);
StringBuilder sb = new StringBuilder();
int pos = 0;
while (ma.find(pos)) {
MatchResult mr = ma.toMatchResult();
int start = mr.start();
int end = mr.end();
pos = end;
sb.append(str.substring(start, end));
}
return sb.toString();
}
use of java.util.regex.MatchResult in project openchemlib by Actelion.
the class StringFunctions method extract.
/**
* @param str
* @param regex
* @return expression which was matched by regex.
*/
public static String extract(String str, String regex) {
String substring = "";
Pattern pa = Pattern.compile(regex);
Matcher ma = pa.matcher(str);
if (ma.find()) {
MatchResult mr = ma.toMatchResult();
substring = mr.group();
}
return substring;
}
use of java.util.regex.MatchResult in project skript-mirror by btk5h.
the class ExprParseRegex method get.
@Override
protected String[] get(Event e) {
List<MatchResult> regexes = ((CustomSyntaxEvent) e).getParseResult().regexes;
if (index < regexes.size()) {
MatchResult match = regexes.get(index);
int groupCount = match.groupCount();
String[] groups = new String[groupCount];
for (int i = 1; i <= groupCount; i++) {
groups[i - 1] = match.group(i);
}
return groups;
}
return new String[0];
}
use of java.util.regex.MatchResult in project syndesis by syndesisio.
the class RegexBasedMasqueradeReader method read.
// This overridden method fills sharedBuf with characters read from in.
@Override
@SuppressWarnings({ "PMD.CyclomaticComplexity", "PMD.ModifiedCyclomaticComplexity", "PMD.StdCyclomaticComplexity", "PMD.NPathComplexity" })
public int read(char[] sharedBuf, int offset, int len) throws IOException {
int off = offset;
// Fetch new line if necessary
if (curLine == null) {
curLine = ((BufferedReader) in).readLine();
curLineIx = 0;
}
// Return characters from current line
if (curLine != null) {
int start = Integer.MAX_VALUE;
int end = Integer.MAX_VALUE;
matcher.reset(curLine);
List<Integer> matches = new ArrayList<>();
while (matcher.find()) {
MatchResult matchResult = matcher.toMatchResult();
matches.add(matchResult.start());
matches.add(matchResult.end());
}
int matchesIdx = 0;
if (!matches.isEmpty()) {
start = matches.get(matchesIdx++);
end = matches.get(matchesIdx++);
}
int num = Math.min(len, Math.min(sharedBuf.length - off, curLine.length() - curLineIx));
// Copy characters from curLine to sharedBuf
for (int i = 0; i < num; i++) {
if (curLineIx <= start || curLineIx > end - 1) {
sharedBuf[off++] = curLine.charAt(curLineIx);
} else {
if (curLineIx == end - 1) {
if (matchesIdx == matches.size()) {
start = Integer.MAX_VALUE;
end = Integer.MAX_VALUE;
} else {
start = matches.get(matchesIdx++);
end = matches.get(matchesIdx++);
}
}
sharedBuf[off++] = '*';
}
curLineIx++;
}
// No more characters in curLine
if (curLineIx == curLine.length()) {
curLine = null;
// Is there room for the newline?
if (num < len && off < sharedBuf.length) {
sharedBuf[off++] = '\n';
emitNewline = false;
num++;
}
}
// Return number of character read
return num;
} else if (emitNewline && len > 0) {
// Emit just the newline
sharedBuf[off] = '\n';
emitNewline = false;
return 1;
} else if (len > 0) {
// No more characters left in input reader
return -1;
} else {
// Client did not ask for any characters
return 0;
}
}
Aggregations