Java Code Examples for java.util.regex.Matcher.toMatchResult()

The following are Jave code examples for showing how to use toMatchResult() of the java.util.regex.Matcher class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: springreplugin   File: DownloadFileInfo.java   View Source Code Vote up 6 votes
/**
 * 根据文件名得到插件名
 *
 * @param fullname
 * @param type
 * @return
 */
public static final String parseName(String fullname, int type) {
    Matcher m = null;
    if (type == INCREMENT_PLUGIN) {
        m = INCREMENT_REGEX.matcher(fullname);
    } else if (type == SINGLE_PLUGIN) {
        m = INCREMENT_SINGLE_REGEX.matcher(fullname);
    } else if (type == MULTI_PLUGIN) {
        m = MULTI_REGEX.matcher(fullname);
    } else {
        m = NORMAL_REGEX.matcher(fullname);
    }
    if (m == null || !m.matches()) {
        return null;
    }
    MatchResult r = m.toMatchResult();
    if (r == null || r.groupCount() != 1) {
        return null;
    }
    return r.group(1);
}
 
Example 2
Project: goobi-viewer-indexer   File: MetadataHelper.java   View Source Code Vote up 6 votes
public static void main(String[] args) {
    String string = "Vol. 15 xxx";
    Pattern p = Pattern.compile(".*(\\d+).*");
    Matcher m = p.matcher(string);

    if (m.find()) {
        MatchResult mr = m.toMatchResult();
        String value = mr.group(1);
        System.out.println("found: " + value);
    }

    p = Pattern.compile("\\d+");
    m = p.matcher(string);
    while (m.find()) {
        try {
            String sub = string.substring(m.start(), m.end());
            System.out.println("found 2: " + sub);
        } catch (NumberFormatException e) {
            logger.error(e.getMessage());
        }
    }
}
 
Example 3
Project: jparsercombinator   File: RegexParserCombinator.java   View Source Code Vote up 5 votes
@Override
public Result<MatchResult> apply(String input) {
  Matcher matcher = pattern.matcher(input);

  if (matcher.lookingAt()) {
    return new Accept<>(matcher.toMatchResult(), input.substring(matcher.end()));
  } else {
    return new Reject<>(input + " does not match " + matcher.pattern());
  }
}
 
Example 4
Project: springreplugin   File: PluginInfo.java   View Source Code Vote up 5 votes
public static final PluginInfo build(File f) {
    Matcher m = REGEX.matcher(f.getName());
    if (m == null || !m.matches()) {
        if (LOG) {
            LogDebug.d(PLUGIN_TAG, "PluginInfo.build: skip, no match1, file=" + f.getAbsolutePath());
        }
        return null;
    }
    MatchResult r = m.toMatchResult();
    if (r == null || r.groupCount() != 4) {
        if (LOG) {
            LogDebug.d(PLUGIN_TAG, "PluginInfo.build: skip, no match2, file=" + f.getAbsolutePath());
        }
        return null;
    }
    String name = r.group(1);
    int low = Integer.parseInt(r.group(2));
    int high = Integer.parseInt(r.group(3));
    int ver = Integer.parseInt(r.group(4));
    String path = f.getPath();
    PluginInfo info = new PluginInfo(name, low, high, ver, TYPE_PN_INSTALLED, DownloadFileInfo.NONE_PLUGIN, path, -1, -1, -1, null);
    if (LOG) {
        LogDebug.d(PLUGIN_TAG, "PluginInfo.build: found plugin, name=" + info.getName()
                + " low=" + info.getLowInterfaceApi() + " high=" + info.getHighInterfaceApi()
                + " ver=" + info.getVersion());
    }
    return info;
}
 
Example 5
Project: guava-mock   File: ArbitraryInstances.java   View Source Code Vote up 4 votes
/**
 * Returns a new {@code MatchResult} that corresponds to a successful match. Apache Harmony (used
 * in Android) requires a successful match in order to generate a {@code MatchResult}:
 * http://goo.gl/5VQFmC
 */
private static MatchResult newMatchResult() {
  Matcher matcher = Pattern.compile(".").matcher("X");
  matcher.find();
  return matcher.toMatchResult();
}
 
Example 6
Project: springreplugin   File: DownloadFileInfo.java   View Source Code Vote up 4 votes
/**
 * 通过文件名和文件类型,构建V5FileInfo对象
 *
 * @param f
 * @param type
 * @return
 */
static final DownloadFileInfo build(File f, int type) {
    Matcher m = null;
    String fullname = f.getName();
    if (type == INCREMENT_PLUGIN) {
        m = INCREMENT_REGEX.matcher(fullname);
    } else if (type == SINGLE_PLUGIN) {
        m = INCREMENT_SINGLE_REGEX.matcher(fullname);
    } else if (type == MULTI_PLUGIN) {
        m = MULTI_REGEX.matcher(fullname);
    } else {
        m = NORMAL_REGEX.matcher(fullname);
    }
    if (m == null || !m.matches()) {
        if (AppConstant.LOG_V5_FILE_SEARCH) {
            if (LOG) {
                LogDebug.d(PLUGIN_TAG, "DownloadFileInfo.build: skip, no match1, type=" + type + " file=" + f.getAbsolutePath());
            }
        }
        return null;
    }
    MatchResult r = m.toMatchResult();
    if (r == null || r.groupCount() != 1) {
        if (AppConstant.LOG_V5_FILE_SEARCH) {
            if (LOG) {
                LogDebug.d(PLUGIN_TAG, "DownloadFileInfo.build: skip, no match2, type=" + type + " file=" + f.getAbsolutePath());
            }
        }
        return null;
    }
    if (!f.exists() || !f.isFile()) {
        if (AppConstant.LOG_V5_FILE_SEARCH) {
            if (LOG) {
                LogDebug.d(PLUGIN_TAG, "DownloadFileInfo.build: nor exist or file, file=" + f.getAbsolutePath());
            }
        }
        return null;
    }
    DownloadFileInfo p = new DownloadFileInfo();
    p.mName = r.group(1);
    p.mFile = f;
    p.mType = type;
    if (LOG) {
        LogDebug.d(PLUGIN_TAG, "DownloadFileInfo.build: found plugin, name=" + p.mName + " file=" + f.getAbsolutePath());
    }
    return p;
}
 
Example 7
Project: springreplugin   File: PluginManager.java   View Source Code Vote up 4 votes
static final int evalPluginProcess(String name) {
    int index = IPluginManager.PROCESS_AUTO;

    try {
        if (TextUtils.equals(IPC.getPackageName(), name)) {
            if (LOG) {
                LogDebug.d(PLUGIN_TAG, "plugin process checker: default, index=" + 0);
            }
            return IPluginManager.PROCESS_UI;
        }

        if (!TextUtils.isEmpty(name)) {
            if (name.contains(PluginProcessHost.PROCESS_PLUGIN_SUFFIX2)) {
                String tail = PluginProcessHost.processTail(name);
                return PluginProcessHost.PROCESS_INT_MAP.get(tail);
            }
        }

        Matcher m = PROCESS_NAME_PATTERN.matcher(name);
        if (m == null || !m.matches()) {
            if (LOG) {
                LogDebug.d(PLUGIN_TAG, "plugin process checker: non plugin process in=" + name);
            }
            return IPluginManager.PROCESS_AUTO;
        }

        MatchResult r = m.toMatchResult();
        if (r == null || r.groupCount() != 2) {
            if (LOG) {
                LogDebug.d(PLUGIN_TAG, "plugin process checker: no group in=" + name);
            }
            return IPluginManager.PROCESS_AUTO;
        }

        String pr = r.group(1);
        if (!TextUtils.equals(IPC.getPackageName(), pr)) {
            if (LOG) {
                LogDebug.d(PLUGIN_TAG, "plugin process checker: package name not match in=" + name);
            }
            return IPluginManager.PROCESS_AUTO;
        }

        String str = r.group(2);
        index = Integer.parseInt(str);
        if (LOG) {
            LogDebug.d(PLUGIN_TAG, "plugin process checker: index=" + index);
        }
    } catch (Throwable e) {
        if (LOG) {
            LogDebug.d(PLUGIN_TAG, e.getMessage(), e);
        }
    }

    return index;
}
 
Example 8
Project: openjdk-jdk10   File: PatternStreamTest.java   View Source Code Vote up 4 votes
MatchResultHolder(Matcher m) {
    this(m.toMatchResult());
}
 
Example 9
Project: googles-monorepo-demo   File: ArbitraryInstances.java   View Source Code Vote up 4 votes
/**
 * Returns a new {@code MatchResult} that corresponds to a successful match. Apache Harmony (used
 * in Android) requires a successful match in order to generate a {@code MatchResult}:
 * http://goo.gl/5VQFmC
 */
private static MatchResult newMatchResult() {
  Matcher matcher = Pattern.compile(".").matcher("X");
  matcher.find();
  return matcher.toMatchResult();
}