0

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?)+)"
jxgn
  • 741
  • 2
  • 15
  • 36
  • `X{1,1}?` is the same as `X{1,1}` is the same as `X{1}` is the same as `X`. – melpomene Jan 03 '17 at 05:44
  • Your regex is crazy. It contains directly nested loops: `([0-9\(]+)*` (all other parts of the inner group are optional, i.e. can match 0 times). It also contains directly adjacent loops with overlapping matches: `[0-9\(]*[0-9\(]+` (the `\.?` in between is optional). In a backtracking regex engine this can loop "forever" (the number of possible combinations this can match in is astronomical). – melpomene Jan 03 '17 at 05:49
  • What implementation of regex are you using - are you really using Qt libraries, as per your tags? If you are using a language please tag that e.g. Python. – halfer Jan 03 '17 at 05:50
  • 1
    It would be much easier if you could explain what exactly you're trying to match, then rewrite the regex from scratch. – melpomene Jan 03 '17 at 05:51
  • @melpomene this is pre implemented and this is only a part of a big regexp. So, I wanted to restrict only that single character rather than messing up with all of it. – jxgn Jan 03 '17 at 06:00
  • 1
    @halfer Sorry about that. I am using Qt. – jxgn Jan 03 '17 at 06:00
  • What do you mean by "this is pre implemented"? – melpomene Jan 03 '17 at 06:02
  • @melpomene like someone implemented this a long time back and I have to fix this one. – jxgn Jan 03 '17 at 06:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/132150/discussion-between-xavier-geoffrey-and-melpomene). – jxgn Jan 03 '17 at 06:05

1 Answers1

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:

((([-]{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.

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

  2. 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 match 5, +, ^, 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.

ssc-hrep3
  • 15,024
  • 7
  • 48
  • 87
  • Yeah, the regexp is part of a big regexp, i have updated my question with the complete regexp. I already tried using the curly braces but it didn't work. – jxgn Jan 04 '17 at 11:15
  • @XavierGeoffrey I updated the answer, but it is still not clear, what you want to achieve. Do you want to force the user to use a dot? – ssc-hrep3 Jan 04 '17 at 12:10