Azerogg
Azerogg

Reputation: 93

How to make an "or" statement in a java Pattern?

So, basically , I am currently working on a program that extracts answers from an html code and stores them into an array. The problem is that when I try to make a pattern in order to separate the answers , I can't seem to make an 'or' statement.

The answers are stored like this on the html code:

['h','e','l','l','o',' ','w','o','r','l','d']

My problem is that when I write it into a String the one with a space(' ') is not recognized by the pattern, so when I write it into a file what shows up is helloworld, with no spaces. What I want to do is a pattern that simultaneously detects the letters AND the spaces , but I have no idea of how to make an 'or' statement in the middle of a pattern.

This is my pattern right now, which only detects the letters:

Pattern p= Pattern.compile("'[A-Z]+'");

EDIT: Still doesn't work...Do you think it might be something else? Here's part of my code( sorry, I know it's a mess):

// creates a String containing the letters separated by ' '
    public static String createString(BufferedReader in,BufferedWriter         out, String texto) throws IOException{
    StringBuilder sb= new StringBuilder();
    Pattern p = Pattern.compile("'[A-Z '']'"); 

        Matcher m= p.matcher(texto);
            while(m.find()){
            sb.append(m.group());
        }
        return sb.toString();
    }
//splits the String in order to create an array with nothing but     letters
public static void toArray(String s, String[] lista, BufferedWriter out) throws IOException{

lista=s.split("[']");
    for(String a:lista){
    out.write(a);
        System.out.print(a); // to check output
    }

}

Upvotes: 1

Views: 179

Answers (2)

xehpuk
xehpuk

Reputation: 8241

Just add a space to the character class:

public class HelloWorldRegex {
    public static void main(final String... args) {
        final String regex = "'([A-Z ])'";
        final Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        final String input = "['h','e','l','l','o',' ','w','o','r','l','d']";
        final Matcher matcher = pattern.matcher(input);
        while (matcher.find()) {
            System.out.print(matcher.group(1));
        }
    }
}

Output: hello world

Test the regex online: https://regex101.com/r/eL8uT9/3

Upvotes: 2

laker02
laker02

Reputation: 117

What you have now only says you're expecting zero or more letters. You need to say you're expecting some letters or a space.

Pattern p = Pattern.compile("'[A-Z]+' | ' ' ");

You need to use the or operator. This way you're saying you're expecting zero or more letters or a space!

Upvotes: -1

Related Questions