mettleap
mettleap

Reputation: 1410

Matching a string which occurs after a certain pattern

I want to match a string which occurs after a certain pattern but I am not able to come up with a regex to do that (I am using Java).

For example, let's say I have this string,

caa,abb,ksmf,fsksf,fkfs,admkf

and I want my regex to match only those commas which are prefixed by abb. How do I do that? Is it even possible using regexes?

If I use the regex abb, it matches the whole string abb, but I only want to match the comma after that.

I ask this because I wanted to use this regex in a split method which accepts a regex. If I pass abb, as the regex, it will consider the string abb, to be the delimiter and not the , which I want.

Any help would be greatly appreciated.

Upvotes: 0

Views: 63

Answers (1)

Talik
Talik

Reputation: 313

        String test = "caa,abb,ksmf,fsksf,fkfs,admkf";
        String regex = "(?<=abb),";
        String[] split = test.split(regex);
        for(String s : split){
            System.out.println(s);
        }

Output:
caa,abb
ksmf,fsksf,fkfs,admkf

See here for information:

https://www.regular-expressions.info/lookaround.html

Upvotes: 2

Related Questions