简体   繁体   English

Java:String.matches()

[英]Java : String.matches()

I want to determine whether a string consists of only two words, with one space character between them. 我想确定一个字符串是否只包含两个单词,它们之间有一个空格字符。 The first word should only include alphanumeric characters. 第一个单词应该只包含字母数字字符。 The second should include only numbers. 第二个应该只包括数字。 An example is: asd 15 一个例子是: asd 15

I would like to use String.matches . 我想使用String.matches How can I do this or which regex should I use? 我该怎么做或者我应该使用哪个正则表达式?

Thanks in advance. 提前致谢。

You look for something like: 你寻找像这样的东西:

String regex = "^[A-Za-z]+ [0-9]+$";

Explanation: 说明:

^         the beginning of the string (nothing before the next token)
[A-Za-z]+ at least one, but maybe more alphabetic chars (case insensitive)
          a single space (hard to see :) )
[0-9]+    at least one, but maybe more digits
$         end of the string (nothing after the digits)

你可以试试这个正则表达式

"[A-Za-z0-9]+\\s[0-9]+"

试试这个: String pattern = new String("^[0-9]*[A-Za-z]+ [0-9]+$")

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

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