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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:48:01+00:00 2026-06-16T18:48:01+00:00

I am trying to create a String[] which contains only words that comprise of

  • 0

I am trying to create a String[] which contains only words that comprise of certain characters. For example I have a dictionary containing a number of words like so:

arm
army
art
as
at
attack
attempt
attention
attraction
authority
automatic
awake
baby
back
bad
bag
balance

I want to narrow the list down so that it only contains words with the characters a, b and g. Therefore the list should only contain the word ‘bag’ in this example.
Currently I am trying to do this using regexes but having never used them before I can’t seem to get it to work.
Here is my code:

public class LetterJugglingMain {
public static void main(String[] args) {
    String dictFile = "/Users/simonrhillary/Desktop/Dictionary(3).txt";
    fileReader fr = new fileReader();
    fr.openFile(dictFile);
    String[] dictionary = fr.fileToArray();
    String regx = "able";
    String[] newDict = createListOfValidWords(dictionary, regx);
    printArray(newDict);
}

public static String[] createListOfValidWords(String[] d, String regex){
    List<String> narrowed = new ArrayList<String>();
    for(int i = 0; i<d.length; i++){
        if(d[i].matches(regex)){
            narrowed.add(d[i]);
            System.out.println("added " + d[i]);
        }
    }
    String[] narrowArray = narrowed.toArray(new String[0]);
    return narrowArray;
}

however the array returned is always empty unless the String regex is the exact word! Any ideas? I can post more code if needed…I think I must be trying to initialise the regex wrong.
The narrowed down list must contain ONLY the characters from the regex.

  • 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-16T18:48:03+00:00Added an answer on June 16, 2026 at 6:48 pm

    The OP wants words that contain every character. Not just one of them.
    And other characters are not a problem.

    If this is the case, I think the simiplest way would be to loop through the entire string, character by character, and check to see if it contains all of the characters you want. Keep flags to check and see if every character has been found.

    If this isn’t the case…. :

    Try using the regex:

    ^[able]+$
    

    Here’s what it does:

    ^ matches the beginning of the string and $ matches the end of the string. This makes sure that you’re not getting a partial match.

    [able] matches the characters you want the string to consist of, in this case a, b, l, and e. + Makes sure that there are 1 or more of these characters in the string.

    Note: This regex will match a string that contains these 4 letters. For example, it will match:

    able, albe, aeble, aaaabbblllleeee

    and will not match

    qable, treatable, and abled.

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

Sidebar

Related Questions

I'm trying to create a string that has a set amount of different words
There is a strange behavior when trying to create string which contains a Hebrew
I'm trying to create a string array with lines of codes, so that the
I am trying to create a table with values that are in a String
I'm trying to create a regex to tokenize a string. An example of the
I'm trying to create a dictionary type - ie hash table with a string
I'm trying to create a JQuery function that will test a string for the
I am trying to create some classes which only contain data members (no functions)
I have a Word document that contains about 4000 form fields which I have
I am trying to append condition to where clause .But the string which contains

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.