In the below example I have a series a characters within the []
which the users are allowed to enter. In this, I want a particular character to be restricted to, say, once.
在下面的示例中,我在[]中有一系列字符,允许用户输入。在这里,我想要将一个特定的角色限制在一次。
For example, I want the user to enter the .
only once. Now, I tried [\.]{1,1}?
but it didn't work.
例如,我希望用户输入。只有一次。现在,我试过[\。] {1,1}?但它不起作用。
"((([-]{1,1}?[0-9\\(]*\\.?[0-9\\(]+[\\+\\-\\*\\/\\)]?)*)|(?:[0-9-+*/^()x\\.?]|<<|>>|!!|&&|[\|]|~|^|abs|e\^x|ln|log|a?(?:sin|cos|tan)h?)+)"
1 个解决方案
#1
0
I assume, you copied the regular expression directly out of your code. The doubly-escaped characters are therefore not needed to analyze your regular expression. They are only there, because you have to escape every \
in a string in e.g. Java. Start by analyzing the following regular expression:
我假设您直接从代码中复制了正则表达式。因此,不需要双重转义字符来分析您的正则表达式。它们只在那里,因为你必须在一个字符串中转义每个\,例如Java的。首先分析以下正则表达式:
((([-]{1,1}?[0-9\(]*\.?[0-9\(]+[\+\-\*\/\)]?)*)|(?:[0-9-+*/^()x\.?]|<<|>>|!!|&&|[\|]|~|^|abs|e\^x|ln|log|a?(?:sin|cos|tan)h?)+)
In your question you mentioned that you want to limit the dot character (.
) to be limited to only one character. The first question is now, which .
do you actually mean? In your regular expression, there are 2 dots.
在您的问题中,您提到要将点字符(。)限制为仅限于一个字符。第一个问题是现在,哪个。你真的是说什么意思?在正则表达式中,有2个点。
((([-]{1,1}?[0-9\(]*\.?[0-9\(]+[\+\-\*\/\)]?)*)|(?:[0-9-+*/^()x\.?]|<<|>>|!!|&&|[\|]|~|^|abs|e\^x|ln|log|a?(?:sin|cos|tan)h?)+)
↑↑↑ (1) ↑↑ (2)
-
The first occurence of your dot is
\.?
. This means, the regular expression matches if there is a dot or there is not a dot.\
escapes the dot, because a.
character would otherwise match every character. The question mark?
means, that the preceding character has to occur between 0 and 1 times. The same is true for this regular expression:\.{0,1}
. They are equal to each other.点的第一次出现是\。?。这意味着,如果存在点或没有点,则正则表达式匹配。 \逃脱了点,因为一个。否则,角色会匹配每个角色。问号?意味着前面的字符必须在0到1次之间发生。这个正则表达式也是如此:\。{0,1}。它们彼此相等。
-
Here, the escaped dot
\.
is part of a set[0-9-+*/^()x\.?]
. This means, any of the characters inside the set has to match exactly one time because there is no quantifier (e.g.+
,*
,?
,{4,12}
) after the set. For example, it would match5
,+
,^
,x
,\.
, but only one time.在这里,逃脱点\。是集合[0-9 - + * / ^()x \。?]的一部分。这意味着,集合中的任何字符必须恰好匹配一次,因为在集合之后没有量词(例如+,*,?,{4,12})。例如,它将匹配5,+,^,x,\。,但只有一次。
The question is now, what do you want to change in this regex? The regular expression is already matching your expectations. You need to provide further information, if your problem is different than here described.
现在的问题是,你想在这个正则表达式中改变什么?正则表达式已经符合您的期望。如果您的问题与此处描述的不同,则需要提供更多信息。
#1
0
I assume, you copied the regular expression directly out of your code. The doubly-escaped characters are therefore not needed to analyze your regular expression. They are only there, because you have to escape every \
in a string in e.g. Java. Start by analyzing the following regular expression:
我假设您直接从代码中复制了正则表达式。因此,不需要双重转义字符来分析您的正则表达式。它们只在那里,因为你必须在一个字符串中转义每个\,例如Java的。首先分析以下正则表达式:
((([-]{1,1}?[0-9\(]*\.?[0-9\(]+[\+\-\*\/\)]?)*)|(?:[0-9-+*/^()x\.?]|<<|>>|!!|&&|[\|]|~|^|abs|e\^x|ln|log|a?(?:sin|cos|tan)h?)+)
In your question you mentioned that you want to limit the dot character (.
) to be limited to only one character. The first question is now, which .
do you actually mean? In your regular expression, there are 2 dots.
在您的问题中,您提到要将点字符(。)限制为仅限于一个字符。第一个问题是现在,哪个。你真的是说什么意思?在正则表达式中,有2个点。
((([-]{1,1}?[0-9\(]*\.?[0-9\(]+[\+\-\*\/\)]?)*)|(?:[0-9-+*/^()x\.?]|<<|>>|!!|&&|[\|]|~|^|abs|e\^x|ln|log|a?(?:sin|cos|tan)h?)+)
↑↑↑ (1) ↑↑ (2)
-
The first occurence of your dot is
\.?
. This means, the regular expression matches if there is a dot or there is not a dot.\
escapes the dot, because a.
character would otherwise match every character. The question mark?
means, that the preceding character has to occur between 0 and 1 times. The same is true for this regular expression:\.{0,1}
. They are equal to each other.点的第一次出现是\。?。这意味着,如果存在点或没有点,则正则表达式匹配。 \逃脱了点,因为一个。否则,角色会匹配每个角色。问号?意味着前面的字符必须在0到1次之间发生。这个正则表达式也是如此:\。{0,1}。它们彼此相等。
-
Here, the escaped dot
\.
is part of a set[0-9-+*/^()x\.?]
. This means, any of the characters inside the set has to match exactly one time because there is no quantifier (e.g.+
,*
,?
,{4,12}
) after the set. For example, it would match5
,+
,^
,x
,\.
, but only one time.在这里,逃脱点\。是集合[0-9 - + * / ^()x \。?]的一部分。这意味着,集合中的任何字符必须恰好匹配一次,因为在集合之后没有量词(例如+,*,?,{4,12})。例如,它将匹配5,+,^,x,\。,但只有一次。
The question is now, what do you want to change in this regex? The regular expression is already matching your expectations. You need to provide further information, if your problem is different than here described.
现在的问题是,你想在这个正则表达式中改变什么?正则表达式已经符合您的期望。如果您的问题与此处描述的不同,则需要提供更多信息。