简体   繁体   English

如何在Java模式中做出“或”语句?

[英]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. 因此,基本上,我目前正在开发一个程序,该程序从html代码中提取答案并将其存储到数组中。 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: 答案像这样存储在html代码上:

['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. 我的问题是,当我将它写入String时,模式无法识别带有空格( ' ' )的字符串,因此,当我将其写入文件时,显示的是helloworld ,没有空格。 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
    }

}

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 输出: hello world

Test the regex online: https://regex101.com/r/eL8uT9/3 在线测试正则表达式: https//regex101.com/r/eL8uT9/3

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! 这样,您说的是期望零个或多个字母或一个空格!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM