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}?[0-9\\(]*\\.?[0-9\\(]+[\\+\\-\\*\\/\\)]?)*)|(?:[0-9-+*/^()x\\.?]|<<|>>|!!|&&|[\|]|~|^|abs|e\^x|ln|log|a?(?:sin|cos|tan)h?)+)"
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: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.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.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.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.