如何用foo或bar检查一行?

时间:2016-04-25 21:53:40

标签: java regex

在我的情况下,我有一些像

这样的词
**********menu 1************
gaurav
saurav
amit
avanish


**********menu 2************
gauravqwe
sourav
anit
abhishek

现在我要检查项目" gaurav"从菜单1或菜单2开始。

如果" gaurav"从菜单1然后返回true,否则返回false

我试试:

class Regex_Test {

    public void checker(String Regex_Pattern){

          Scanner Input = new Scanner(System.in);
          String Test_String = Input.nextLine();
          Pattern p = Pattern.compile(Regex_Pattern);
          Matcher m = p.matcher(Test_String);
          System.out.println(m.find());
   }   
}


public class CheckHere {    
    public static void main(String[] args) {    
        Regex_Test tester = new Regex_Test();
        tester.checker("^[gsa][amv]"); // Use \\ instead of using \ 
    }
} 

但是在" gauravqwe"
的情况下它返回true 我需要表达" string"对于上述问题 条件字符串大小小于15个字符

2 个答案:

答案 0 :(得分:0)

使用元字符边界\b

\bbgaurav\b

regex101 Demo

参考文献:

http://www.regular-expressions.info/wordboundaries.html

答案 1 :(得分:0)

少于15个字符

要在少于15个字符的情况下执行此操作,您需要一个如下所示的正则表达式:

^gaurav(\r|\Z)

此正则表达式是以下答案的子集。

描述

我通过构建正则表达式获取匹配条目的菜单标题,一次性完成。这个正则表达式将执行以下操作:

  • 在源字符串中找到gaurav
  • 从匹配部分
  • 返回菜单名称
  • 如果字符串不包含匹配项,则返回将为空

正则表达式

[*]{9,}([^*]*?)[*]{9,}(?:(?![*]{9,}).)*^gaurav(?:\r|\Z)

Regular expression visualization

请注意,此正则表达式使用以下选项:不区分大小写,多行(使用^和$匹配开始和结束行),并且点匹配新行(使用。匹配\ n)

<强>解释

这个构造(?:(?![*]{9,}).)*是所有魔法发生的地方。这会强制搜索在字符串中向前移动,但不允许模式匹配跨越多个**********分隔的段。

^(?:\n|\Z)构造强制正则表达式引擎匹配完整字符串,而不仅仅是初始字符。示例:如果您正在寻找gaurav,那么gauravqwe将无法匹配。

NODE                     EXPLANATION
----------------------------------------------------------------------
  [*]{9,}                  any character of: '*' (at least 9 times
                           (matching the most amount possible))
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    [^*]*?                   any character except: '*' (0 or more
                             times (matching the least amount
                             possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  [*]{9,}                  any character of: '*' (at least 9 times
                           (matching the most amount possible))
----------------------------------------------------------------------
  (?:                      group, but do not capture (0 or more times
                           (matching the most amount possible)):
----------------------------------------------------------------------
    (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
      [*]{9,}                  any character of: '*' (at least 9
                               times (matching the most amount
                               possible))
----------------------------------------------------------------------
    )                        end of look-ahead
----------------------------------------------------------------------
    .                        any character
----------------------------------------------------------------------
  )*                       end of grouping
----------------------------------------------------------------------
  ^                        the beginning of a "line"
----------------------------------------------------------------------
  gaurav                   'gaurav'
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    \r                       '\r' (carriage return)
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    \Z                       before an optional \n, and the end of
                             the string
----------------------------------------------------------------------
  )                        end of grouping

Java代码示例

import java.util.regex.Pattern;
import java.util.regex.Matcher;
class Module1{
  public static void main(String[] asd){
  String sourcestring = "source string to match with pattern";
  Pattern re = Pattern.compile("[*]{9,}([^*]*?)[*]{9,}(?:(?![*]{9,}).)*^gaurav(?:\\r|\\Z)",Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
  Matcher m = re.matcher(sourcestring);
  int mIdx = 0;
    while (m.find()){
      for( int groupIdx = 0; groupIdx < m.groupCount()+1; groupIdx++ ){
        System.out.println( "[" + mIdx + "][" + groupIdx + "] = " + m.group(groupIdx));
      }
      mIdx++;
    }
  }
}

<强>返回

$matches Array:
(
    [0] => Array
        (
            [0] => **********menu 1************
gaurav
        )

    [1] => Array
        (
            [0] => menu 1
        )

)