Regex matching beginning AND end strings

前端 未结 5 531
长情又很酷
长情又很酷 2020-12-05 06:50

This seems like it should be trivial, but I\'m not so good with regular expressions, and this doesn\'t seem to be easy to Google.

I need a regex that starts with the

相关标签:
5条回答
  • 2020-12-05 07:05

    If you're searching for hits within a larger text, you don't want to use ^ and $ as some other responders have said; those match the beginning and end of the text. Try this instead:

    \bdbo\.\w+_fn\b
    

    \b is a word boundary: it matches a position that is either preceded by a word character and not followed by one, or followed by a word character and not preceded by one. This regex will find what you're looking for in any of these strings:

    dbo.functionName_fn
    foo dbo.functionName_fn bar
    (dbo.functionName_fn)
    

    ...but not in this one:

    foodbo.functionName_fnbar
    

    \w+ matches one or more "word characters" (letters, digits, or _). If you need something more inclusive, you can try \S+ (one or more non-whitespace characters) or .+? (one or more of any characters except linefeeds, non-greedily). The non-greedy +? prevents it from accidentally matching something like dbo.func1_fn dbo.func2_fn as if it were just one hit.

    0 讨论(0)
  • 2020-12-05 07:05
    ^dbo\..*_fn$
    

    This should work you.

    0 讨论(0)
  • 2020-12-05 07:12
    Scanner scanner = new Scanner(System.in);
    String part = scanner.nextLine();
    String line = scanner.nextLine();
    
    String temp = "\\b" + part +"|"+ part + "\\b";
    Pattern pattern = Pattern.compile(temp.toLowerCase());
    Matcher matcher = pattern.matcher(line.toLowerCase());
    
    System.out.println(matcher.find() ? "YES":"NO");
    

    If you need to determine if any of the words of this text start or end with the sequence. you can use this regex \bsubstring|substring\b anythingsubstring substringanything anythingsubstringanything

    0 讨论(0)
  • 2020-12-05 07:20

    Well, the simple regex is this:

    /^dbo\..*_fn$/
    

    It would be better, however, to use the string manipulation functionality of whatever programming language you're using to slice off the first four and the last three characters of the string and check whether they're what you want.

    0 讨论(0)
  • 2020-12-05 07:21
    \bdbo\..*fn
    

    I was looking through a ton of java code for a specific library: car.csclh.server.isr.businesslogic.TypePlatform (although I only knew car and Platform at the time). Unfortunately, none of the other suggestions here worked for me, so I figured I'd post this.

    Here's the regex I used to find it:

    \bcar\..*Platform
    
    0 讨论(0)
提交回复
热议问题