Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8839271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:12:09+00:00 2026-06-14T10:12:09+00:00

I am trying to create a regex for a CodingBat problem where I have

  • 0

I am trying to create a regex for a CodingBat problem where I have to return a boolean based on if a String contains the phrase ‘xyz’, but ‘xyz’s that are prefeaced with a period don’t count. My attempted regexes so far have been:

str.matches("(?<!\\.)xyz");
str.matches("[^\\.]xyz")

but neither works the way I intend. If someone could steer me in the right direction as to get this to work, I would be thankful.

EDIT: Since someone asked for the tests:

xyzThere(“abcxyz”) → true
xyzThere(“abc.xyz”) → false
xyzThere(“xyz.abc”) → true
xyzThere(“abcxy”) → false
xyzThere(“xyz”) → true
xyzThere(“xy”) → false
xyzThere(“x”) → false
xyzThere(“”) → false
xyzThere(“abc.xyzxyz”) → true
xyzThere(“abc.xxyz”) → true
xyzThere(“.xyz”) → false
xyzThere(“12.xyz”) → false
xyzThere(“12xyz”) → true
xyzThere(“1.xyz.xyz2.xyz”) → false

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:12:10+00:00Added an answer on June 14, 2026 at 10:12 am

    matches will test against the entire string (implicitly adding ^ and $ around the pattern). A quick fix is to add .* to both the beginning and the end of the pattern. Otherwise your first attempt is the correct one. The second one would fail at the beginning of the string, because it requires one non-. character (but there has to be one).

    public boolean xyzThere(String str) {
        return str.matches(".*(?<!\\.)xyz.*");
    }
    

    This, admittedly is a bit of a hack. To do the usual regex testing against any part of the string, you need to use a bit more OOP:

    public boolean xyzThere(String str) {
        return java.util.regex.Pattern.compile("(?<!\\.)xyz").matcher(str).find();
    }
    

    (I couldn’t seem to use import on CodingBat.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a regex that will return false if the String
I'm trying to create a regex that matches the inverse of a certain string
I'm trying to create a regex expression to match any string that has exactly
I'm trying to create a regex that accept: An empty string, a single integer
I'm trying to create a regex that will match only when the string has
I'm trying to create a regex that will return true when the content type
I'm trying to create a Regex test in JavaScript that will test a string
I'm trying to create a regex that will matches with 8-9. but won't matches
I'm trying to create a regex that reads a string, and if the last
I'm trying to create a regex to tokenize a string. An example of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.