Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中捕获字符的存在性_Java_Regex - Fatal编程技术网

如何在Java中捕获字符的存在性

如何在Java中捕获字符的存在性,java,regex,Java,Regex,我的基本意图是找出一个dir,比如说a是否直接在dirb中。我在java中检查它,所以我使用模式和匹配器类 假设homeDir=/aa/bb/cc homeDir dirB=/aa/bb/cc/dd/ee未立即进入主目录 dirC=/aa/bb/cc/dd直接在主目录内 dirD=/aa/ff外部主目录 如果dir直接在homeDir中,我希望输出为true,否则为false。我的方法是检查homeDir路径是否与给定的dir路径匹配。如果匹配,我将检查homeDir部分之后是否有/。这是我的示

我的基本意图是找出一个dir,比如说a是否直接在dirb中。我在java中检查它,所以我使用模式和匹配器类

假设homeDir=/aa/bb/cc homeDir dirB=/aa/bb/cc/dd/ee未立即进入主目录 dirC=/aa/bb/cc/dd直接在主目录内 dirD=/aa/ff外部主目录

如果dir直接在homeDir中,我希望输出为true,否则为false。我的方法是检查homeDir路径是否与给定的dir路径匹配。如果匹配,我将检查homeDir部分之后是否有/。这是我的示例代码

private static final String REGEX =
                             "homeDir"+"/"+"((.+)(/?)(.+))";
private static final String INPUT =
                            "/aa/bb/cc/dd/ee";

public static void main(String[] args) {
   Pattern p = Pattern.compile(REGEX);
   Matcher m = p.matcher(INPUT); // get a matcher object
   boolean output = false;
   while(m.find()) {
      System.out.println("Total : "+m.group(1)+"First : "+m.group(2)+ "Slash : "+m.group(3)+"Last : "+m.group(4));
            if(m.group(3).length()==0)
            {// that means match found and immediately inside also
              output = true;

            }
   }
我最讨厌的是 总计:dd/eeFirst:dd/eSlash:Last:e。我想要的是,如果存在斜杠的话,我想捕获它。我认为首先,+是贪婪的,把一切都拿走了。我试过使用所有格量词/?+但没有成功 线程主java.util.regex.PatternSyntaxException中的异常:在正则表达式模式中的位置3处: 试图重复已重复的令牌 /aa/bb/cc/+/?++ ^
请帮助我解决这个问题

首先,您应该使用File而不是RegEx,因为它可以解析所有别名文件,如“~”或“.”或“…”

Pattern p = Pattern.compile("/aa/bb/cc/([^/]+)");
Matcher m = p.matcher(input);
boolean output = m.matches();
if (output)
  System.out.println(m.group(1));
第二,如果您确定没有别名,另一种方法是减法和包含


String  aFileName    = "...";
boolean aIsImmedient = aFileName.substring(aHomeDir.length).contain('/');
我之所以推荐这两个,是因为它们比正则表达式快

如果您仍然需要正则表达式,请使用“homeDir/[^/]+/[^/]+”,您的打印将是:


System.out.print("Total : %s; First: %s; Last: %s"
    m.group(0), // All match
    m.group(1), // First  '(..)'
    m.group(2)  // Second '(..)'
);
单独使用“/”而不使用“…”和“?”,使用“[^/]”而不是/因为您希望它只在那里证明一个“/”的匹配,而不是更多


希望这有帮助。

如果您只想知道一个文件/文件夹是否位于另一个文件/文件夹中,则不需要REGEXP

public static boolean isImmediateParent(String parentPath, String childPath)
{
    int index = childPath.lastIndexOf("/");
    if (index == -1)
        throw new IllegalArgumentException("child has no parent");
    return childPath.substring(0, index).equals(parentPath);
}

isImmediateParent("a", "a") = exception
isImmediateParent("a", "a/b") = true
isImmediateParent("a", "a/b/c") = false
isImmediateParent("a", "b/a") = false

基本正确。但是,1必须使用getCanonicalPath,2不能解析~,3根据操作系统平台解析符号链接和/或驱动器名,以及4。和严格来说不是别名。嘿,斯蒂芬·科伯特,你是对的,关于getCanonicalPath,我会解决这个问题Dyeah,尽管要记住在不同的操作系统上使用不同的文件分隔符-为了处理这个问题,我会在开始时对两个参数都使用.replaceAll\\\\,/这比使用合适的分隔符更简单。
public static boolean isImmediateParent(String parentPath, String childPath)
{
    int index = childPath.lastIndexOf("/");
    if (index == -1)
        throw new IllegalArgumentException("child has no parent");
    return childPath.substring(0, index).equals(parentPath);
}

isImmediateParent("a", "a") = exception
isImmediateParent("a", "a/b") = true
isImmediateParent("a", "a/b/c") = false
isImmediateParent("a", "b/a") = false