1

I have trouble with compiling a code that converts an infix expression to a postfix expression. I suspect the following two lines of code to be giving me the issue:

public static final Pattern UNSIGNED_DOUBLE = Pattern.compile("\\b[\\+-]?[0-9]*[\\.]?[0-9]+([eE][\\+-]?[0-9]+)?\\b");

 public static final Pattern CHARACTER = Pattern.compile("\\S.*?");

Can someone please help me correct the two lines of code to avoid the unclosed group near index 1?

Thanks

user207421
  • 305,947
  • 44
  • 307
  • 483
Rafay Adil
  • 11
  • 1
  • Welcome to Stack Overflow! Please [edit] your question to include the exact issue you are having, with the full text of the error message where appropriate. – Joe C Nov 26 '18 at 21:27
  • Your regex is fine, see [this](https://regex101.com/r/1AogXq/1) and [this](https://regex101.com/r/1AogXq/2) demos. Though the second pattern is most probably some human error. – Wiktor Stribiżew Nov 26 '18 at 21:32

1 Answers1

0

If you're having trouble matching the +/- at the beginning, use something like
this

(?<![-+.\da-zA-Z_])[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?\b

https://regex101.com/r/EJII3S/1

Where (?<![-+.\da-zA-Z_]) is basically a word boundary
that is appropriate for the possible symbols . + -

Explained

 (?<! [-+.\da-zA-Z_] )         # Word and symbol boundary
 [+-]?                         # Optional +/-
 (?:
      \d+                           # Starts with a digit
      (?: \. \d* )?
   |                              # or,
      \. \d+                        # Starts with a decimal point
 )
 (?: [eE] [+-]? \d+ )?         # Optional exponent
 \b                            # Just a word boundary
  • Just as a general comment, a monstrosity like this is far less clear than just writing a parser, or using an existing parser or tokenizer. – markspace Nov 26 '18 at 21:53
  • @markspace - I added a [formatted/commented](http://www.regexformat.com) version if you cant read it. And I can parse anything with regex. –  Nov 26 '18 at 22:00
  • That's really not the point. regex should be avoided (like the plague) in production code. It's always worse than just writing code. I'd much rather deal with a parser. And this isn't aimed at you specifically, you're just answering the question. My comment is aimed at everyone in general who thinks dropping complicated regex into production code is a good idea. – markspace Nov 26 '18 at 22:03
  • @markspace - I've stopped trying to convert people to regex. But, it actually is the point. Regex is _code_ like any other code. If you can't read it, you can't write it. New powerful tools do the work for you. Btw, this is a large regex [175,000 word Dictionary](http://www.regexformat.com/Dnl/_Samples/_Ternary_Tool%20(Dictionary)/___txt/_ASCII_175,000_word_Mix_A-Z_Multi_Lined.txt) that is faster than anything you could write by other code. –  Nov 26 '18 at 22:12
  • @markspace - The [nay sayer's](https://stackoverflow.com/questions/53299385/email-id-validation-according-to-rfc5322-and-https-en-wikipedia-org-wiki-email/53382093#53382093) are everywhere. But, it's not going to change the fact that regex usage is on the rise. Better catch up, or be left out. –  Nov 26 '18 at 22:20