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 6211395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:19:47+00:00 2026-05-24T06:19:47+00:00

Trying to learn regular expressions. As a practice, I’m trying to find every word

  • 0

Trying to learn regular expressions. As a practice, I’m trying to find every word that appears exactly one time in my document — in linguistics this is a hapax legemenon (http://en.wikipedia.org/wiki/Hapax_legomenon)

So I thought the following expression give me the desired result:

\w{1}

But this doesn’t work. The \w returns a character not a whole word. Also it does not appear to be giving me characters that appear only once (it actually returns 25873 matches — which I assume are all alphanumeric characters). Can someone give me an example of how to find “hapax legemenon” with a regular expression?

  • 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-24T06:19:48+00:00Added an answer on May 24, 2026 at 6:19 am

    If you’re trying to do this as a learning exercise, you picked a very hard problem 🙂

    First of all, here is the solution:

    \b(\w+)\b(?<!\b\1\b.*\b\1\b)(?!.*\b\1\b)
    

    Now, here is the explanation:

    • We want to match a word. This is \b\w+\b – a run of one or more (+) word characters (\w), with a ‘word break’ (\b) on either side. A word break happens between a word character and a non-word character, so this will match between (e.g.) a word character and a space, or at the beginning and the end of the string. We also capture the word into a backreference by using parentheses ((...)). This means we can refer to the match itself later on.

    • Next, we want to exclude the possibility that this word has already appeared in the string. This is done by using a negative lookbehind – (?<! ... ). A negative lookbehind doesn’t match if its contents match the string up to this point. So we want to not match if the word we have matched has already appeared. We do this by using a backreference (\1) to the already captured word. The final match here is \b\1\b.*\b\1\b – two copies of the current match, separated by any amount of string (.*).

    • Finally, we don’t want to match if there is another copy of this word anywhere in the rest of the string. We do this by using negative lookahead – (?! ... ). Negative lookaheads don’t match if their contents match at this point in the string. We want to match the current word after any amount of string, so we use (.*\b\1\b).

    Here is an example (using C#):

    var s = "goat goat leopard bird leopard horse";
    
    foreach (Match m in Regex.Matches(s, @"\b(\w+)\b(?<!\b\1\b.*\b\1\b)(?!.*\b\1\b)"))
        Console.WriteLine(m.Value);
    

    Output:

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

Sidebar

Related Questions

I am trying to learn more about regular expressions I have one below that
I'm trying to learn from reading Mozilla documentation for regular expressions, but there's one
While trying to learn a little more about regular expressions, a tutorial suggested that
I'm currently trying to learn regular expressions with some simple "real world" examples. Take
I'm new to regular expressions and grep, but I've been trying to learn it
I'm new to regular expressions, but I'm trying to learn about it. I want
I'm trying to learn some basic regular expression and having a hard time getting
Trying to learn Regex in Python to find words that have consecutive vowel-consonant or
I'm trying to learn about regular expressions but am not doing so well after
I'm really bad with regular expressions - never had enough time to learn it

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.