Java正则表达式“\\ d [a-zA-z]?”

时间:2016-04-19 10:33:10

标签: java regex

我在Java中有这个正则表达式:\\d[a-zA-z]

它应该只允许字母后跟1或0字符。

然而,当我将它与例如17c甚至21匹配时,它不会导致匹配。这是我使用的代码:

if (!(pattern.matches("\\d[a-zA-z]?"))) {
      Throw error...;
}

感谢您的帮助!

3 个答案:

答案 0 :(得分:2)

那是因为17c21"\d[a-zA-z]?"不匹配。此表达式匹配一个数字后跟一个字符。

尝试使用匹配一个或多个数字后跟零个或多个字符的"\d+[a-zA-z]*"

答案 1 :(得分:1)

  

它应该只允许字母后跟1或0字符。

试试这个正则表达式:

   <profile>
        <id>ecj</id>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>copy-compiler</id>
                            <goals>
                                <goal>copy</goal>
                            </goals>
                            <phase>compile</phase>
                            <configuration>
                                <artifactItems>
                                    <artifactItem>
                                        <artifactId>lombok</artifactId>
                                        <groupId>org.projectlombok</groupId>
                                        <version>${lombok.version}</version>
                                    </artifactItem>
                                    <artifactItem>
                                        <groupId>org.eclipse.jdt.core.compiler</groupId>
                                        <artifactId>ecj</artifactId>
                                        <version>4.5.1</version>
                                    </artifactItem>
                                </artifactItems>
                                <stripVersion>true</stripVersion>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>ecj-compile</id>
                            <goals>
                                <goal>exec</goal>
                            </goals>
                            <phase>compile</phase>
                            <configuration>
                                <executable>java</executable>
                                <classpathScope>compile</classpathScope>
                                <arguments>
                                    <argument>-javaagent:target/dependency/lombok.jar</argument>
                                    <argument>-jar</argument>
                                    <argument>target/dependency/ecj.jar</argument>
                                    <argument>-d</argument>
                                    <argument>none</argument>
                                    <argument>-properties</argument>
                                    <argument>.settings/org.eclipse.jdt.core.prefs</argument>
                                    <argument>-cp</argument>
                                    <classpath />
                                    <argument>${project.build.sourceDirectory}</argument>
                                </arguments>
                            </configuration>
                        </execution>
                        <execution>
                            <id>ecj-test-compile</id>
                            <goals>
                                <goal>exec</goal>
                            </goals>
                            <phase>test-compile</phase>
                            <configuration>
                                <executable>java</executable>
                                <classpathScope>test</classpathScope>
                                <arguments>
                                    <argument>-javaagent:target/dependency/lombok.jar</argument>
                                    <argument>-jar</argument>
                                    <argument>target/dependency/ecj.jar</argument>
                                    <argument>-d</argument>
                                    <argument>none</argument>
                                    <argument>-properties</argument>
                                    <argument>.settings/org.eclipse.jdt.core.prefs</argument>
                                    <argument>-cp</argument>
                                    <classpath />
                                    <argument>${project.build.testSourceDirectory}</argument>
                                </arguments>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

下面:

  • [a-zA-Z]+.? 至少匹配一个字母(大写和小写)
  • [a-zA-Z]+匹配任何字符(换行符除外)零次或一次。

答案 2 :(得分:0)

可能性\ d + [a-zA-Z]?你正在寻找的那个