Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将给定的输入字符串与正则表达式列表匹配_Java_Regex - Fatal编程技术网

Java 将给定的输入字符串与正则表达式列表匹配

Java 将给定的输入字符串与正则表达式列表匹配,java,regex,Java,Regex,我正在开发一个程序,将给定的建筑字符串转换为十进制英寸。例如:12.5'表示12英尺6英寸。现在,我还没有做任何转换 如何测试给定字符串是否与模式数组列表中的任何模式匹配? 以下是我所拥有的: // List of Patterns String wDec = "((\\d+)\\.(\\d+))\\'"; // 12.5' String numberWithDoubleQuotes = "^(\\d+)\\\""; // 11" String inche

我正在开发一个程序,将给定的建筑字符串转换为十进制英寸。例如:12.5'表示12英尺6英寸。现在,我还没有做任何转换

如何测试给定字符串是否与模式数组列表中的任何模式匹配? 以下是我所拥有的:

        // List of Patterns
    String wDec = "((\\d+)\\.(\\d+))\\'"; // 12.5'
    String numberWithDoubleQuotes = "^(\\d+)\\\""; // 11"
    String inchesWithForwardDash = "(\\d+)\\/(\\d+)\\\""; // 3/16"

    // Spaces may or may not be used between the feet and inches and the inches and
    // 16ths
    String feetSQSpaceInchesDQ = "(\\d+)\\'(\\s)?(\\d+)\\\""; // 11' 11" OR 11'11"

    // Dashes may or may not be used between feet and inches or between inches and
    // 16ths or both
    String wDash = "(\\d+)\\'(\\-)?(\\d+)\\\""; // 12'-11"
    String wSpacesForwardDash = "(\\d+)\\'\\s+(\\d+)\\s((\\d+)\\/(\\d+))\\\""; // 12' 11 3/16"
    String wSpacesDashForwardDash = "(\\d+)\\'\\s+(\\d+)\\-((\\d+)\\/(\\d+))\\\""; // 12' 11-1/2"

    // Any number of spaces may be used between the feet and inches and the inches
    // and 16ths
    String multipleSpaceForwardDash = "(\\d+)\\'\\s+(\\d+)\\s+((\\d+)\\/(\\d+))\\\""; // 12' 11 1/2"

    // An alternate simpler format using only a contiguous (no spaces) string of
    // digits is also common
    String threeGroupContiguous = "(\\d{2})(\\d{2})(\\d{2})"; // 121103
    String twoGroupContiguous = "^(\\d{2})(\\d{2})"; // 1103
    String oneGroupContiguous = "^(\\d{2})\\b"; // 03

    List<Pattern> patterns = new ArrayList<>();
    patterns.add(Pattern.compile(wDec));
    patterns.add(Pattern.compile(numberWithDoubleQuotes));
    patterns.add(Pattern.compile(inchesWithForwardDash));
    patterns.add(Pattern.compile(feetSQSpaceInchesDQ));
    patterns.add(Pattern.compile(wDash));
    patterns.add(Pattern.compile(wSpacesForwardDash));
    patterns.add(Pattern.compile(wSpacesDashForwardDash));
    patterns.add(Pattern.compile(multipleSpaceForwardDash));
    patterns.add(Pattern.compile(threeGroupContiguous));
    patterns.add(Pattern.compile(twoGroupContiguous));
    patterns.add(Pattern.compile(oneGroupContiguous));
//模式列表
字符串wDec=“((\\d+)\。(\\d+)\ \”;//12.5'
字符串numberWithDoubleQuotes=“^(\\d+\\\”;//11”
字符串inchesWithForwardDash=“(\\d+\\\/(\\d+\\\\”;//3/16”
//英尺和英寸以及英寸和英寸之间可以使用空格,也可以不使用空格
//十六分之一
字符串feetSQSpaceInchesDQ=“(\\d+\\\”(\\s)?(\\d+\\\”;//11'11”或11'11”
//在英尺和英寸之间或英寸和英寸之间可以使用破折号,也可以不使用破折号
//16分之一或两者兼而有之
字符串wDash=“(\\d+\\”(\\-)?(\\d+\\\”;//12'-11"
字符串wSpacesForwardDash=“(\\d+\ \”\\s+(\\d+)\\s+(\\d+\ \/(\\d+)\ \ \ \ \”;//12' 11 3/16"
字符串wSpacesDashForwardDash=“(\\d+\ \”\\s+(\\d+\ \ \ \-(\\d+\ \/(\\d+)\ \ \ \ \”;//12' 11-1/2"
//英尺和英寸以及英寸之间可以使用任意数量的空间
//和16
字符串multipleSpaceForwardDash=“(\\d+\ \”\\s+(\\d+)\\s+(\\d+\ \/(\\d+)\ \ \ \”;//12' 11 1/2"
//仅使用连续(无空格)字符串的另一种更简单的格式
//数字也很常见
String threegroupcontinuous=“(\\d{2})(\\d{2})(\\d{2})”;//121103
字符串TwoGroupContinental=“^(\\d{2})(\\d{2})”;//1103
字符串OneGroupContinental=“^(\\d{2})\\b”//03
列表模式=新的ArrayList();
patterns.add(Pattern.compile(wDec));
patterns.add(Pattern.compile(numberWithDoubleQuotes));
patterns.add(Pattern.compile(inchesWithForwardDash));
patterns.add(Pattern.compile(feetSQSpaceInchesDQ));
patterns.add(Pattern.compile(wDash));
patterns.add(Pattern.compile(wSpacesForwardDash));
patterns.add(Pattern.compile(wSpacesDashForwardDash));
patterns.add(Pattern.compile(multipleSpaceForwardDash));
patterns.add(Pattern.compile(threegroupcontinuous));
patterns.add(Pattern.compile(TwoGroupContinental));
patterns.add(Pattern.compile(onegroupcontinuous));
谢谢!

导入org.junit.Before;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import static org.junit.Assert.*;

@RunWith(JUnit4.class)
public class PatternTest {

    private List<Pattern> patterns;

    @Before
    public void setUp() {
        // List of Patterns
        String wDec = "((\\d+)\\.(\\d+))\\'"; // 12.5'
        String numberWithDoubleQuotes = "^(\\d+)\\\""; // 11"
        String inchesWithForwardDash = "(\\d+)\\/(\\d+)\\\""; // 3/16"

        // Spaces may or may not be used between the feet and inches and the inches and
        // 16ths
        String feetSQSpaceInchesDQ = "(\\d+)\\'(\\s)?(\\d+)\\\""; // 11' 11" OR 11'11"

        // Dashes may or may not be used between feet and inches or between inches and
        // 16ths or both
        String wDash = "(\\d+)\\'(\\-)?(\\d+)\\\""; // 12'-11"
        String wSpacesForwardDash = "(\\d+)\\'\\s+(\\d+)\\s((\\d+)\\/(\\d+))\\\""; // 12' 11 3/16"
        String wSpacesDashForwardDash = "(\\d+)\\'\\s+(\\d+)\\-((\\d+)\\/(\\d+))\\\""; // 12' 11-1/2"

        // Any number of spaces may be used between the feet and inches and the inches
        // and 16ths
        String multipleSpaceForwardDash = "(\\d+)\\'\\s+(\\d+)\\s+((\\d+)\\/(\\d+))\\\""; // 12' 11 1/2"

        // An alternate simpler format using only a contiguous (no spaces) string of
        // digits is also common
        String threeGroupContiguous = "(\\d{2})(\\d{2})(\\d{2})"; // 121103
        String twoGroupContiguous = "^(\\d{2})(\\d{2})"; // 1103
        String oneGroupContiguous = "^(\\d{2})\\b"; // 03

         patterns = new ArrayList<>();
        patterns.add(Pattern.compile(wDec));
        patterns.add(Pattern.compile(numberWithDoubleQuotes));
        patterns.add(Pattern.compile(inchesWithForwardDash));
        patterns.add(Pattern.compile(feetSQSpaceInchesDQ));
        patterns.add(Pattern.compile(wDash));
        patterns.add(Pattern.compile(wSpacesForwardDash));
        patterns.add(Pattern.compile(wSpacesDashForwardDash));
        patterns.add(Pattern.compile(multipleSpaceForwardDash));
        patterns.add(Pattern.compile(threeGroupContiguous));
        patterns.add(Pattern.compile(twoGroupContiguous));
        patterns.add(Pattern.compile(oneGroupContiguous));
    }

    @Test
    public void testInput11(){
        String input = "input1"; //your input here

        assertTrue("Input should match at least once: ", matches(input));

    }

    private boolean matches(String input){
        return patterns.stream().anyMatch( pattern -> pattern.matcher(input).matches());
    }
}
导入org.junit.Test; 导入org.junit.runner.RunWith; 导入org.junit.runners.JUnit4; 导入java.util.ArrayList; 导入java.util.List; 导入java.util.regex.Pattern; 导入静态org.junit.Assert.*; @RunWith(JUnit4.class) 公共类模式测试{ 私有列表模式; @以前 公共作废设置(){ //模式列表 字符串wDec=“((\\d+)\。(\\d+)\ \”;//12.5 字符串numberWithDoubleQuotes=“^(\\d+\\\”;//11” 字符串inchesWithForwardDash=“(\\d+\\\/(\\d+\\\\”;//3/16” //英尺和英寸以及英寸和英寸之间可以使用空格,也可以不使用空格 //十六分之一 字符串feetSQSpaceInchesDQ=“(\\d+\\\”(\\s)?(\\d+\\\”;//11'11”或11'11” //在英尺和英寸之间或英寸和英寸之间可以使用破折号,也可以不使用破折号 //16分之一或两者兼而有之 字符串wDash=“(\\d+\ \”(\\-)?(\\d+\ \”;//12'-11” 字符串wSpacesForwardDash=“(\\d+\ \”\\s+(\\d+)\\s+(\\d+\/(\\d+)\ \ \ \”;//12'11 3/16” 字符串wSpacesDashForwardDash=“(\\d+\ \”\\s+(\\d+\ \ \ \-(\\d+\/(\\d+)\ \ \ \ \”;//12'11-1/2” //英尺和英寸以及英寸之间可以使用任意数量的空间 //和16 字符串multipleSpaceForwardDash=“(\\d+\ \”\\s+(\\d+)\\s+(\\d+\/(\\d+)\ \ \ \”;//12'11 1/2” //仅使用连续(无空格)字符串的另一种更简单的格式 //数字也很常见 String threegroupcontinuous=“(\\d{2})(\\d{2})(\\d{2})”;//121103 字符串TwoGroupContinental=“^(\\d{2})(\\d{2})”;//1103 字符串OneGroupContinental=“^(\\d{2})\\b”//03 patterns=newarraylist(); patterns.add(Pattern.compile(wDec)); patterns.add(Pattern.compile(numberWithDoubleQuotes)); patterns.add(Pattern.compile(inchesWithForwardDash)); patterns.add(Pattern.compile(feetSQSpaceInchesDQ)); patterns.add(Pattern.compile(wDash)); patterns.add(Pattern.compile(wSpacesForwardDash)); patterns.add(Pattern.compile(wSpacesDashForwardDash)); patterns.add(Pattern.compile(multipleSpaceForwardDash)); patterns.add(Pattern.compile(threegroupcontinuous)); patterns.add(Pattern.compile(TwoGroupContinental)); patterns.add(Pattern.compile(onegroupcontinuous)); } @试验 公共无效测试输入11(){ String input=“input1”//此处输入 assertTrue(“输入应至少匹配一次:”,匹配(输入)); } 专用布尔匹配(字符串输入){ 返回patterns.stream().anyMatch(pattern->pattern.matcher(input.matches()); } }