# Having difficulty with EReg in Java

**URL:** <https://community.haxe.org/t/having-difficulty-with-ereg-in-java/2005>\
**Category:** Haxe\
**Tags:** haxe-java\
**Created:** [September 4, 2019, 12:03pm UTC](https://community.haxe.org/t/having-difficulty-with-ereg-in-java/2005 "2019-09-04T12:03:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marika-0](https://community.haxe.org/user_avatar/community.haxe.org/marika-0/32/994_2.png) [@Marika-0](https://community.haxe.org/u/Marika-0)\
**Post date:** [September 4, 2019, 12:03pm UTC](https://community.haxe.org/t/having-difficulty-with-ereg-in-java/2005/1 "2019-09-04T12:03:39Z")

</div>

I’m having trouble with a regex when compiling to Java. The regex is meant to match ANSI sequences in strings so that I can split the string with the regex and join the resulting array with `""`, removing ANSI formatting.

This is the Haxe regex:

```haxe
~/[\x1b\x9b][[\]()#;?]*((([a-zA-Z0-9]*(;[-a-zA-Z0-9\/#&.:=?%@~_]*)*)?\x07)|(([0-9][0-9]?[0-9]?[0-9]?(;[0-9]?[0-9]?[0-9]?[0-9]?)*)?[0-9A-PR-TZcf-ntqry=><~]))/g

```

This is the compiled java output:

```java
new haxe.root.EReg("[\\x1b\\x9b][[\\]()#;?]*((([a-zA-Z0-9]*(;[-a-zA-Z0-9/#&.:=?%@~_]*)*)?\\x07)|(([0-9][0-9]?[0-9]?[0-9]?(;[0-9]?[0-9]?[0-9]?[0-9]?)*)?[0-9A-PR-TZcf-ntqry=><~]))", "g")

```

And this is the exception I get thrown:

```haxe
Exception in thread "main" java.lang.ExceptionInInitializerError
    <stack tracing>
Caused by: java.util.regex.PatternSyntaxException: Unclosed character class near index 152
[\x1b\x9b][[\]()#;?]*((([a-zA-Z0-9]*(;[-a-zA-Z0-9/#&.:=?%@~_]*)*)?\x07)|(([0-9][0-9]?[0-9]?[0-9]?(;[0-9]?[0-9]?[0-9]?[0-9]?)*)?[0-9A-PR-TZcf-ntqry=><~]))
                                                                                                                                                        ^
    <more stack tracing>

```

[Apparently](https://stackoverflow.com/questions/17623094/java-util-regex-patternsyntaxexception-unclosed-character-class-near-index) this is caused by me not escaping something, probably in that last set of `[]`.

Does anyone have experience with Java regexes?

This is happening with Haxe 4.0.0-rc.3, hxjava 3.2.0 & 4.0.0-alpha, openjdk-8-jdk + openjdk-8-jre, and openjdk-11-jdk + openjdk-11-jre; and I’m on Ubuntu 18.04, if it helps.

Thanks!

---

<div class="post-metadata">

**Author:** ![sundialservices](https://avatars.discourse-cdn.com/v2/letter/s/9e8a1a/32.png) [@sundialservices](https://community.haxe.org/u/sundialservices)\
**Post date:** [September 6, 2019, 3:01pm UTC](https://community.haxe.org/t/having-difficulty-with-ereg-in-java/2005/2 "2019-09-06T15:01:48Z")

</div>

Escaping strings can be very tricky when you’re building a regex. I’d suggest that you _display_ the string that you’re right now passing to the EReg method, and look for tpyos. 🙂

I’ve also been known to break down a complicated regular-expression string into several concatenated constants, just to make things easier to read for the programmer … even if it’s just me. As you see, such “chicken scratch strings” can be unreadable.
