Search code examples
javaregexformal-languages

Java regex match number of times substring/char appears in string


I'm writing a program to recognize valid (as defined by the user) expressions, and I need to be able to match the number of times a specific character matches in one part with another. For example, if I'm using the pq system, in

(some number of dashes)p-q(some number of dashes)

the number of dashes before the p has to match the number of dashes after the q (for any number of dashes) for it to be a valid expression.

I'm trying to make this robust/generalizable (user-defined rules, you can use * instead of -, you can make it so that you need double the amount of dashes after the q, etc), I was thinking adding some special text (like -(%%n%%)p-q-(%%n%%) where anything having (%%n%%) after it has to appear exactly 'n' times), but before I do that are there any built-in regex capabilities/reasonably simple fixes or am I going to need to start with my %%'s?

Thanks in advance.


Edit: Looking at my problem again, the better way to word it is that I essentially need to figure out a way to mark two (or possibly more) substrings (in a general way) to verify that they match. I have some degree of flexibility in how I do it (not solely limited to regex), but the rest of the system uses regex so something that doesn't conflict/massively overcomplicate what I'd need to do to get it to work would be ideal.


Solution

  • If I got your problem right, regex will be not sufficient, as you are trying to catch string generated by non-regular grammar. Some simple pushdown automaton should help here.