Faisal
Faisal

Reputation: 443

To check if a pattern exists in a String

I tried searching but could not find anything that made any sense to me! I am noob at regex :)

Trying to see if a particular word "some_text" exists in another string.

String s = "This is a test() function"
String s2 = "This is a test    () function"

Assuming the above two strings I can search this using the following pattern at RegEx Tool

[^\w]test[ ]*[(]

But unable to get a positive match in Java using

System.out.println(s.matches("[^\\w]test[ ]*[(]");

I have tried with double \ and even four \\ as escape characters but nothing really works.

The requirement is to see the word starts with space or is the first word of a line and has an open bracket "(" after that particular word, so that all these "test (), test() or test ()" should get a positive match.

Using Java 1.8

Cheers, Faisal.

Upvotes: 4

Views: 4273

Answers (5)

The fourth bird
The fourth bird

Reputation: 163467

The .matches method will match the whole string where your pattern would only get a partial match.

In the pattern that you tried, the negated character class [^\\w] could also match more than a whitespace boundary as it matches any char except a word character. It could for example also match a ( or a newline.

As per the comments test() function should also match, using [^\\w] or (?<=\s) expects a character to be there on the left.


Instead you could make use of (?<!\\S) to assert a whitespace boundary on the left.

.*(?<!\S)test\h*\(.*

Explanation

  • .* Match 0+ times any char except a newline
  • (?<!\S) Assert a whitespace boundary on the left
  • test\h* Match test and 0+ horizontal whitespace chars
  • \( Match a ( char
  • .* Match 0+ times any char except a newline

Regex demo | Java demo

In Java

System.out.println(s.matches(".*(?<!\\S)test\\h*\\(.*"));

Upvotes: 1

vs97
vs97

Reputation: 5859

The point you are missing is that Java matches() puts a ^ at the start and a $ at the end of the Regex for you. So your expression actually is seen as:

^[^\w]test[ ]*[(]$

which is never going to match your input.

Going from your requirement description, I suggest reworking your regex expression to something like this (assuming by "particular word" you meant test):

(?:.*)(?<=\s)(test(?:\s+)?\()(?:.*)

See the regex at work here.

Explanation:

^                 Start of line - added by matches()
(?:.*)            Non-capturing group - match anything before the word, but dont capture into a group
(?<=\s)           Positive lookbehind - match if word preceded by space, but dont match the space
(                 Capturing group $1
  test(?:\s+)?    Match word test and any following spaces, if they exist
  \(              Match opening bracket
)                 
(?:.*)            Non-capturing group - match rest of string, but dont capture in group
$                 End of line - added by matches()

Code sample:

public class Main {
    public static void main(String[] args) {
        String s = "This is a test() function";
        String s2 = "This is a test    () function";
        System.out.println(s.matches("(?:.*)((?<=\\s))(test(?:\\s+)?\\()(?:.*)")); 
        //true
    }
}

Upvotes: 5

JvdV
JvdV

Reputation: 75900

The Matches() method tells whether or not this whole string matches the given regular expression. Since that's not the case you'll yield errors.

If you just interested in if your lookup-value exists within the string I found the following usefull:


import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Main {
  public static void main(String[] args) {
    String s = "This is a test    () function";
    Pattern p = Pattern.compile("\\btest *\\(");
    Matcher m = p.matcher(s);
    if (m.find())
      System.out.println("Found a match");
    else
      System.out.println("Did not find a match");
  }
}

I went with the following pattern: \\btest *\\(

  • \\b - Match word-boundary (will also catch if first word).
  • test - Literally match your lookup-value.
  • * - Zero or more literal spaces.
  • \\( - Escaped open paranthesis to match literally.

Regular expression visualization

Debuggex Demo

Upvotes: 2

SG Tech Edge
SG Tech Edge

Reputation: 507

Try this "\btest\b(?= *()".

And dont use "matches", use "find". Mathes trying to match the whole string

https://regex101.com/r/xaPCyp/1

Upvotes: 2

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236114

I believe this should be enough:

s.find("\\btest\\s*\\(")

Upvotes: 2

Related Questions