Example usage for java.util.regex Matcher groupCount

List of usage examples for java.util.regex Matcher groupCount

Introduction

In this page you can find the example usage for java.util.regex Matcher groupCount.

Prototype

public int groupCount() 

Source Link

Document

Returns the number of capturing groups in this matcher's pattern.

Usage

From source file:net.leegorous.jsc.JavaScriptDocument.java

public static List getImportInfo(String content) {
    List config = null;/*from  ww w.  j  a v  a 2 s .  com*/
    Matcher m = IMPORT_PATTERN.matcher(content);

    if (m.find()) {
        config = new ArrayList();
        do {
            if (m.groupCount() > 1) {
                config.add(m.group(1));
            }
        } while (m.find());
    }
    return config;
}

From source file:AIR.Common.Web.BrowserParser.java

static double ParseVersion(String userAgent, Pattern regex) {
    double version = 0;

    Matcher matches = regex.matcher(userAgent);

    boolean result = matches.find();

    if (result && matches.groupCount() >= 1) {
        String value = matches.group(1);
        if (!StringUtils.isEmpty(value)) {
            value = value.replace('_', '.');
            version = Double.parseDouble(value);
        }/*w w  w.  java  2 s  . c om*/
    }

    return version;
}

From source file:com.grarak.romswitcher.Utils.Utils.java

public static String formatKernelVersion(String rawKernelVersion) {

    final String PROC_VERSION_REGEX = "Linux version (\\S+) " + "\\((\\S+?)\\) " + "(?:\\(gcc.+? \\)) "
            + "(#\\d+) " + "(?:.*?)?" + "((Sun|Mon|Tue|Wed|Thu|Fri|Sat).+)";

    Matcher m = Pattern.compile(PROC_VERSION_REGEX).matcher(rawKernelVersion);
    if (!m.matches()) {
        Log.e("RomSwitcher", "Regex did not match on /proc/version: " + rawKernelVersion);
        return "Unavailable";
    } else if (m.groupCount() < 4) {
        Log.e("RomSwitcher", "Regex match on /proc/version only returned " + m.groupCount() + " groups");
        return "Unavailable";
    }//from w  ww .j a  v  a2 s  . com
    return m.group(1);
}

From source file:org.apache.hadoop.hbase.io.HFileLink.java

/**
 * @param fileName File name to check./*from   ww w.j  a  v  a2 s. c o m*/
 * @return True if the path is a HFileLink.
 */
public static boolean isHFileLink(String fileName) {
    Matcher m = LINK_NAME_PATTERN.matcher(fileName);
    if (!m.matches())
        return false;
    return m.groupCount() > 2 && m.group(4) != null && m.group(3) != null && m.group(2) != null;
}

From source file:com.avapira.bobroreader.hanabira.HanabiraParser.java

private static String replaceInternalLinkWithReference(String message) {
    Pattern p = Pattern/*from www  . j  a v  a  2 s.  c  o  m*/
            .compile("https?://dobrochan\\.(?:ru|com|org)/([a-z]{1,4})/res/(\\d+)\\.xhtml(?:#i(\\d+))?");
    Matcher matcher = p.matcher(message);
    if (matcher.groupCount() == 4) { // whole, board, thread, (opt) post
        return matcher.replaceAll(">>$1/$3"); //>>/board/display_id_post
    } else {
        return matcher.replaceAll(">>$1/$2"); //>>/board/thread_display_id
    }
}

From source file:com.ery.ertc.estorm.util.ToolUtil.java

public static String ReplaceRegex(Matcher m, String substitution) {
    try {//from   w w w.  ja va  2  s  .  com
        Matcher vm = valPartsRegex.matcher(substitution);
        String val = substitution;
        String regpar = substitution;
        int gl = m.groupCount();
        while (vm.find()) {
            regpar = regpar.substring(vm.end());
            int g = Integer.parseInt(vm.group(1));
            if (g > gl) {
                val = val.replaceAll("\\$\\d", "");
                break;
            }
            String gv = m.group(Integer.parseInt(vm.group(1)));
            if (gv != null)
                val = val.replaceAll("\\$" + g, gv);
            else
                val = val.replaceAll("\\$" + g, "");
            vm = valPartsRegex.matcher(regpar);
        }
        return val;
    } catch (Exception e) {
        return null;
    }
}

From source file:fr.ign.cogit.geoxygene.appli.plugin.script.ScriptingPrimitiveRenderer.java

/**
 * Replace all 'include "YYY"' lines by the YYY file content
 * @param fileContent file content where to replace includes contents
 * @return preprocessed content/*from www . j a  v a2s  . com*/
 * @throws RenderingException
 */
private static String preprocessInclude(final String filename, final String fileContent)
        throws RenderingException {
    if (fileContent == null) {
        return null;
    }
    try {
        Pattern includePattern = Pattern.compile("include\\s+[\\\"\\\'](.*)[\\\"\\\']", Pattern.MULTILINE);
        // Pattern includePattern = Pattern.compile("^\\s*include",
        // Pattern.MULTILINE);
        Matcher m = includePattern.matcher(fileContent);

        if (m.find() && m.groupCount() == 1) {
            return preprocessInclude(filename,
                    fileContent.replace(m.group(0), preprocessScriptingFile(new File(m.group(1)))));
        }
    } catch (PatternSyntaxException pse) {
        throw new RenderingException(pse);
    }
    return fileContent;
}

From source file:org.ballerinalang.langserver.command.CommandUtil.java

/**
 * Get the command instances for a given diagnostic.
 *
 * @param diagnostic Diagnostic to get the command against
 * @param params     Code Action parameters
 * @return {@link List}     List of commands related to the given diagnostic
 *//*  w  w w  .  j  a va2 s  .  c o  m*/
public static List<Either<Command, CodeAction>> getCommandsByDiagnostic(Diagnostic diagnostic,
        CodeActionParams params) {
    String diagnosticMessage = diagnostic.getMessage();
    List<Either<Command, CodeAction>> commands = new ArrayList<>();
    Position position = params.getRange().getStart();
    CommandArgument lineArg = new CommandArgument(CommandConstants.ARG_KEY_NODE_LINE, "" + position.getLine());
    CommandArgument colArg = new CommandArgument(CommandConstants.ARG_KEY_NODE_COLUMN,
            "" + position.getCharacter());
    CommandArgument uriArg = new CommandArgument(CommandConstants.ARG_KEY_DOC_URI,
            params.getTextDocument().getUri());

    if (isUndefinedPackage(diagnosticMessage)) {
        String packageAlias = diagnosticMessage.substring(diagnosticMessage.indexOf("'") + 1,
                diagnosticMessage.lastIndexOf("'"));
        LSDocument sourceDocument = new LSDocument(params.getTextDocument().getUri());
        String sourceRoot = LSCompilerUtil.getSourceRoot(sourceDocument.getPath());
        sourceDocument.setSourceRoot(sourceRoot);
        List<BallerinaPackage> packagesList = new ArrayList<>();
        Stream.of(LSPackageLoader.getSdkPackages(), LSPackageLoader.getHomeRepoPackages())
                .forEach(packagesList::addAll);
        packagesList.stream().filter(pkgEntry -> {
            String fullPkgName = pkgEntry.getFullPackageNameAlias();
            return fullPkgName.endsWith("." + packageAlias) || fullPkgName.endsWith("/" + packageAlias);
        }).forEach(pkgEntry -> {
            String commandTitle = CommandConstants.IMPORT_MODULE_TITLE + " "
                    + pkgEntry.getFullPackageNameAlias();
            CommandArgument pkgArgument = new CommandArgument(CommandConstants.ARG_KEY_MODULE_NAME,
                    pkgEntry.getFullPackageNameAlias());
            commands.add(Either.forLeft(new Command(commandTitle, CommandConstants.CMD_IMPORT_MODULE,
                    new ArrayList<>(Arrays.asList(pkgArgument, uriArg)))));
        });
    } else if (isUndefinedFunction(diagnosticMessage)) {
        List<Object> args = Arrays.asList(lineArg, colArg, uriArg);
        String functionName = "";
        Matcher matcher = CommandConstants.UNDEFINED_FUNCTION_PATTERN.matcher(diagnosticMessage);
        if (matcher.find() && matcher.groupCount() > 0) {
            functionName = matcher.group(1) + "(...)";
        }
        String commandTitle = CommandConstants.CREATE_FUNCTION_TITLE + functionName;
        commands.add(Either.forLeft(new Command(commandTitle, CommandConstants.CMD_CREATE_FUNCTION, args)));
    } else if (isVariableAssignmentRequired(diagnosticMessage)) {
        List<Object> args = Arrays.asList(lineArg, colArg, uriArg);
        String commandTitle = CommandConstants.CREATE_VARIABLE_TITLE;
        commands.add(Either.forLeft(new Command(commandTitle, CommandConstants.CMD_CREATE_VARIABLE, args)));
    } else if (isUnresolvedPackage(diagnosticMessage)) {
        Matcher matcher = CommandConstants.UNRESOLVED_MODULE_PATTERN
                .matcher(diagnosticMessage.toLowerCase(Locale.ROOT));
        if (matcher.find() && matcher.groupCount() > 0) {
            List<Object> args = new ArrayList<>();
            String pkgName = matcher.group(1);
            args.add(new CommandArgument(CommandConstants.ARG_KEY_MODULE_NAME, pkgName));
            args.add(uriArg);
            String commandTitle = CommandConstants.PULL_MOD_TITLE;
            commands.add(Either.forLeft(new Command(commandTitle, CommandConstants.CMD_PULL_MODULE, args)));
        }
    }
    return commands;
}

From source file:com.Candy.center.AboutCandy.java

public static String formatKernelVersion(String rawKernelVersion) {

    final String PROC_VERSION_REGEX = "Linux version (\\S+) " + /* group 1: "3.0.31-g6fb96c9" */
            "\\((\\S+?)\\) " + /* group 2: "x@y.com" (kernel builder) */
            "(?:\\(gcc.+? \\)) " + /* ignore: GCC version information */
            "(#\\d+) " + /* group 3: "#1" */
            "(?:.*?)?" + /* ignore: optional SMP, PREEMPT, and any CONFIG_FLAGS */
            "((Sun|Mon|Tue|Wed|Thu|Fri|Sat).+)"; /* group 4: "Thu Jun 28 11:02:39 PDT 2012" */

    Matcher m = Pattern.compile(PROC_VERSION_REGEX).matcher(rawKernelVersion);
    if (!m.matches()) {
        Log.e(LOG_TAG, "Regex did not match on /proc/version: " + rawKernelVersion);
        return "Unavailable";
    } else if (m.groupCount() < 4) {
        Log.e(LOG_TAG, "Regex match on /proc/version only returned " + m.groupCount() + " groups");
        return "Unavailable";
    }/*from w  w  w.ja v  a2  s .  c  o m*/
    return m.group(1) + " " + // 3.0.31-g6fb96c9
            m.group(2) + " " + m.group(3);
}

From source file:com.gs.obevo.db.apps.reveng.AquaRevengMain.java

private static Pair<Integer, String> getStartIndex(String str, Pattern p) {
    Matcher m = p.matcher(str);
    while (m.find()) {
        String objectName = m.groupCount() > 0 ? m.group(1) : null; // by convention, the second group collected has the name
        return Tuples.pair(m.start(), objectName);
    }/*w ww  . j a  v  a2s.  c  o  m*/
    return Tuples.pair(Integer.MAX_VALUE, null);
}