Reverse regex in Java

How to cancel regex in Java? For example, 'ab. + De '=>' ed. + Ba '.

+4
source share
3 answers

Wow.

You need to create a parser for the regular expression and undo all the markers / parts.

in this case

ab. + de is

a, b ,. +, d, e

and vice versa:

e, d ,. +, b, a

now imagine groups

((ab) (. + De))

back side

((ed. +), (Ba))

+4
source

In fact, it would be much easier to turn over a haystack than a needle. And since Matcher accepts CharSequence instead of String , you can do this with trivial overhead by simply wrapping String (see Answers to Back String in Java, in O (1)? ).

With this knowledge, you can create an alternative version of Matcher that may look like the opposite picture, but actually just reverses the input.

+4
source

Tiago Peczenyj is correct, and you need to handle both backlinks, capture groups, and named groups. Named groups, because in Java RegEx there is no restriction that a named group needs to return a link by name, it can be returned by number, like any other capture group.

If anyone is interested in a Java solution, I have implemented a library to do just that. https://github.com/vsch/reverse-regex .

Handles all valid regex Java constructors and provides utility classes for wrapping, matching, and input for reverse lookups to handle all the necessary mappings and calls.

0
source

Source: https://habr.com/ru/post/1411911/


All Articles