-2

I'm trying to construct a regex that will match a string, when there are n or more occurrences of a particular character (anywhere in string).

E.g. the character is /, and n=2

Would give:

test -> false
/test -> false
/test/test -> true
/test/test/test -> true

How best to do this?

UpTheCreek
  • 31,444
  • 34
  • 152
  • 221
  • There are a lot of suchlike questions here, did you try any of the solutions? – Wiktor Stribiżew Oct 28 '21 at 16:35
  • @WiktorStribiżew There are many regex questions, but I haven't found this particular question. If you have please could you share the link. – UpTheCreek Oct 28 '21 at 16:37
  • See https://stackoverflow.com/a/52420739/3832970, just one of tens I have just come across. – Wiktor Stribiżew Oct 28 '21 at 16:38
  • Thanks, I'll check that and close if duplicate. – UpTheCreek Oct 28 '21 at 16:40
  • The technique is simple: match the char, then use a group that matches any chars other than the char and then the char, and quantify the group as you see fit. – Wiktor Stribiżew Oct 28 '21 at 16:41
  • Whether it will work or not depends also on where and how you use the regex. Please share your current code. [Do not rely](https://stackoverflow.com/questions/39636124/regular-expression-works-on-regex101-com-but-not-on-prod) on regex101.com testing. – Wiktor Stribiżew Oct 28 '21 at 17:21

3 Answers3

0

You can use backreference:

(/).*\1.*\1
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34
  • Interesting thanks. Testing that in regex101 now, but haven't got it to work yet. (I'm assuming the forward slash needs escaping). – UpTheCreek Oct 28 '21 at 16:43
  • no foward slashes are not metachars – Chris Ruehlemann Oct 28 '21 at 16:45
  • @UpTheCreek This is the kind of feedback you will get if you do not provide 1) your programming language, 2) the code + regex pattern you are currently trying to use. Chris has a rich R background, and in R, regexps are defined with string literals, not regex literals, so backslashes need no escaping there. – Wiktor Stribiżew Oct 28 '21 at 17:31
0

Or you can use capturing groups:

public static void main(final String args[]) throws Exception
{
    final String input = "/test/test/test";

    final String toCount = "/";
    final int n = 2;

    System.out.println("Looking for ".concat(n + "x: \"".concat(toCount).concat("\"")));
    System.out.println("RESULT for \"".concat(input).concat("\": ") + filterChars(input, toCount, n));
}

public static boolean filterChars(final String s, final String toCount, final int n)
{
    // Oh boy, this is the best... string concatenation to create regex patterns... what could go wrong?
    // Please... think of the children
    final Pattern p = Pattern.compile("(" + toCount + ")");
    final Matcher m = p.matcher(s);

    int count = 0;
    while (m.find())
    {
        count++;
    }

    return (n <= count);
}
JonathanDavidArndt
  • 2,518
  • 13
  • 37
  • 49
0

You can use a capturing lookahead:

/^(?=(\/)(?:.*\1){1,}).*/gm

Demo

dawg
  • 98,345
  • 23
  • 131
  • 206