Java 一个或多个字母/数字和零个或多个空格的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23091427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex for ONE-or-more letters/digits And ZERO-or-more spaces
提问by vplusplus
I want to allow 0 or more white spaces in my string and one or more A-Z or a-z or 0-9 in my string.
我想在我的字符串中允许 0 个或多个空格,并在我的字符串中允许一个或多个 AZ 或 az 或 0-9。
Regex allowing a space character in Java
suggests [0-9A-Za-z ]+
.
建议[0-9A-Za-z ]+
。
I doubt that, this regex matches patterns having zero or more white spaces.
我怀疑,这个正则表达式匹配具有零个或多个空格的模式。
What to do to allow 0 or more whitespaces anywhere in the string and one or more characters anywhere in the string.
如何在字符串中的任意位置允许 0 个或多个空格以及在字符串中的任意位置允许一个或多个字符。
Will this work? ([0-9A-Za-z]+)([ ]*)
这会起作用吗? ([0-9A-Za-z]+)([ ]*)
采纳答案by Cruncher
I believe you can do something like this:
我相信你可以做这样的事情:
([ ]*+[0-9A-Za-z]++[ ]*+)+
This is 0 or more spaces, followed by at least 1 alphanum char, followed by 0 or more spaces
这是 0 个或多个空格,后跟至少 1 个字母字符,后跟 0 个或多个空格
^^ that whole thing at least once.
^^整件事至少一次。
Using Pshemo's idea of possessive quantifiers to speed up the regex.
使用 Pshemo 的所有格量词的想法来加速正则表达式。
回答by Toto
Use lookahead:
使用前瞻:
^(?=.*\s*)(?=.*[a-zA-Z0-9]+)[a-zA-Z0-9 ]+$
回答by Justin
You are asking that the string (s
) satisfies this condition (note: let c∈s
mean c∈{x|x is a character in s}
. Also, []
represent regex character classes):
您要求字符串 ( s
) 满足此条件(注意: let c∈s
mean c∈{x|x is a character in s}
。另外,[]
表示正则表达式字符类):
(?c∈s (c∈[0-9A-Za-z ])) ∧ (?c∈s ? c∈[0-9A-Za-z])
Consider the negation:
考虑否定:
?((?c∈s c∈[0-9A-Za-z ]) ∧ (?c∈s ? c∈[0-9A-Za-z]))
?
(?c∈s ? c?[0-9A-Za-z ]) ∨ (?c∈s c?[0-9A-Za-z])
?
(?c∈s ? c∈[^0-9A-Za-z ]) ∨ (?c∈s c∈[^0-9A-Za-z])
So now we want to construct a regex that either contains a non-alphanumeric and non-space character or consists only of non-alphanumeric characters.
所以现在我们要构造一个包含非字母数字和非空格字符或仅包含非字母数字字符的正则表达式。
The first is easy: [^0-9A-Za-z ]
.
The second is like unto it: ^[^0-9A-Za-z]*$
第一个很简单:[^0-9A-Za-z ]
。
第二个就像它:^[^0-9A-Za-z]*$
Combine them together to get: [^0-9A-Za-z ]|^[^0-9A-Za-z]*$
将它们组合在一起得到: [^0-9A-Za-z ]|^[^0-9A-Za-z]*$
Now we need to negate this regex. Obviously, we could just do (?![^0-9A-Za-z ]|^[^0-9A-Za-z]*$)
. Or we could manually negate the regex:
现在我们需要否定这个正则表达式。显然,我们可以只做(?![^0-9A-Za-z ]|^[^0-9A-Za-z]*$)
. 或者我们可以手动否定正则表达式:
[^0-9A-Za-z ]
becomes ^[0-9A-Za-z ]*$
^[^0-9A-Za-z]*$
becomes [0-9A-Za-z]
. (note: we could easily have arrived here from the beginning)
[^0-9A-Za-z ]
变成^[0-9A-Za-z ]*$
^[^0-9A-Za-z]*$
变成[0-9A-Za-z]
。(注意:我们很容易从一开始就到达这里)
But now we need to combine them with AND, not OR:
但是现在我们需要将它们与 AND 而不是 OR 结合起来:
Since [0-9A-Za-z]
is a subset of [0-9A-Za-z ]
, we can simply do this:
由于[0-9A-Za-z]
是 的子集[0-9A-Za-z ]
,我们可以简单地这样做:
^[0-9A-Za-z ]*[0-9A-Za-z][0-9A-Za-z ]*$
Note that we can simplify it down to:
请注意,我们可以将其简化为:
^[0-9A-Za-z ]*[0-9A-Za-z][ ]*$
This just requires that the character that matches [0-9A-Za-z]
is the last character that could do so. We could also do
这仅要求匹配[0-9A-Za-z]
的字符是可以这样做的最后一个字符。我们也可以做
^[ ]*[0-9A-Za-z][0-9A-Za-z ]*$
Which would require that the character that matches [0-9A-Za-z]
is the first character that could do so.
这将要求匹配的字符[0-9A-Za-z]
是第一个可以这样做的字符。
So now we're done. We can either use one of those or (?![^0-9A-Za-z ]|^[^0-9A-Za-z]*$)
.
所以现在我们完成了。我们可以使用其中之一或(?![^0-9A-Za-z ]|^[^0-9A-Za-z]*$)
.
Note: String#match
acts as if the regex is ^ + regex + $
(where +
is concatenation). This can throw a few things off.
注意:String#match
就像正则表达式一样^ + regex + $
(其中+
是连接)。这可能会导致一些问题。
回答by donut
You can try also this :
你也可以试试这个:
^[0-9A-Za-z ]*[0-9A-Za-z]+[ ]*$
回答by aliteralmind
Before looking at the other answers, I came up with doing it in two regexes:
在查看其他答案之前,我想出了两个正则表达式:
boolean ok = (myString.matches("^[A-Za-z0-9 ]+$") && !myString.matches("^ *$"));
This matches one-or-more letters/digits andzero-or-more spaces, but notonly spaces (or nothing).
这符合一个或更多的字母/数字和零或更多的空间,但不是唯一的空格(或没有)。
It could be made efficient by pre-creating a single matcher object for each regex:
可以通过为每个正则表达式预先创建一个匹配器对象来提高效率:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class OnePlusLetterDigitZeroPlusSpace {
//"": Unused search string, to reuse the matcher object
private static final Matcher mtchr1PlusLetterDigitSpc = Pattern.compile("^[a-zA-z0-9 ]+$").matcher("");
private static final Matcher mtchr0PlusSpc = Pattern.compile("^ *$").matcher("");
public static final void main(String[] ignored) {
test("");
test(" ");
test("a");
test("hello ");
test(" hello ");
test("hello there");
}
private static final void test(String to_search) {
System.out.print("\"" + to_search + "\": ");
if(mtchr1PlusLetterDigitSpc.reset(to_search).matches() && !mtchr0PlusSpc.reset(to_search).matches()) {
System.out.println("good");
} else {
System.out.println("BAD");
}
}
}
Output:
输出:
[C:\java_code\]java OnePlusLetterDigitZeroPlusSpace
"": BAD
" ": BAD
"a": good
"hello ": good
" hello ": good
"hello there": good
Interesting regex question of the day.
当天有趣的正则表达式问题。
回答by CONvid19
回答by Basheer AL-MOMANI
The most simple answer
最简单的答案
*
means zero or more
equivalent to {0,}
*
意味着zero or more
相当于 {0,}
+
means one or more
equivalent to {1,}
+
意味着one or more
相当于 {1,}
so look at this
所以看看这个
[A-Z]+
means at least one Capital Letter
, can be written as [A-Z]{1,}
[A-Z]+
意味着at least one Capital Letter
,可以写成[A-Z]{1,}
[!@#$%&].
means you can have these Special Characters zero or more times
can be written as [!@#$%&]{0,}
[!@#$%&].
意味着您可以将这些特殊字符zero or more times
写为[!@#$%&]{0,}
sorry but
对不起,可是
the
purpose
of thisanswer
to beas Simple as possible
在
purpose
此answer
要as Simple as possible