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

  • Home
  • SEARCH
  • 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 4563446
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:21:34+00:00 2026-05-21T18:21:34+00:00

How to construct a regex not to contain a set of strings within. For

  • 0

How to construct a regex not to contain a set of strings within.

For this example, I want to validate the Address Line 1 text box so that it wont contain any secondary address parts such ‘Apt’, ‘Bldg’,’Ste’,’Unit’ etc.

  • 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-05-21T18:21:35+00:00Added an answer on May 21, 2026 at 6:21 pm

    A regex can be used to verify that a string does not contain a set of words. Here is a tested Java code snippet with a commented regex which does precisely this:

    if (s.matches("(?sxi)" +
        "# Match string containing no 'bad' words.\n" +
        "^                # Anchor to start of string.\n" +
        "(?:              # Step through string one char at a time.\n" +
        "  (?!            # Negative lookahead to exclude words.\n" +
        "    \\b          # All bad words begin on a word boundary\n" +
        "    (?:          # List of 'bad' words NOT to be matched.\n" +
        "      Apt        # Cannot be 'Apt',\n" +
        "    | Bldg       # or 'Bldg',\n" +
        "    | Ste        # or 'Ste',\n" +
        "    | Unit       # or 'Unit'.\n" +
        "    )            # End list of words NOT to be matched.\n" +
        "    \\b          # All bad words end on a word boundary\n" +
        "  )              # Not at the beginning of bad word.\n" +
        "  .              # Ok. Safe to match this character.\n" +
        ")*               # Zero or more 'not-start-of-bad-word' chars.\n" +
        "$                # Anchor to end of string.")
        ) {
        // String has no bad words.
        System.out.print("OK: String has no bad words.\n");
    } else {
        // String has bad words.
        System.out.print("ERR: String has bad words.\n");
    } 
    

    This assumes that the words must be “whole” words and that the “bad” words should be recognized regardless of case. Note also, (as others have correctly stated), that this is not as efficient as simply checking for the presence of bad words and then taking the logical NOT.

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

Sidebar

Related Questions

If I want to construct a std::string with a line like: std::string my_string(a\0b); Where
I have a simple problem: I want to construct a regex that matches a
How to construct two regex patterns into one? For example I have one long
I can't figure out how to construct a regex for the example values: 123,456,789
Is is possible to construct a regular expression that rejects all input strings?
How can I construct my ajaxSend call, this seems like the place to put
I am trying to match a series of text strings with PCRE on PHP,
I am trying to construct a regex to find a string in ruby str
Given a string, I want to replace all links within it with the link's
I have a regex expression that I'm trying to construct that processes a file

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.