Reputation: 3008
I am trying to capture a group from regex that will follow this pattern:
Ex1 - anyanyany
group 1 have to be anyanyany
Ex2 - anyanyany.abcany
group 1 have to be anyanyany
Ex3 - anyany.abcde.fghi
group 1 have to be anyany.abcde
When I try (.+)(?:\.)
, it only returns Ex2 and Ex3. If I change it for (.+)(?:\.)*
it returns the same string of input.
I really don't know what I have to do to solve it. Someone could help me? Which knowledgement I am missing?
https://regex101.com/r/jG6wY8/2
Upvotes: 0
Views: 61
Reputation: 1884
Try the following regex patterns in order to match your criterias:
If you were supposed to match the first two words tokenized by a dot: ^([^\.]+)(?:\.[^\.]+)?$|(?:([^\.]+\.[^\.]+)\.)
a => a
a.b => a
a.b.c => a.b
a.b.c.d => a.b
If you were supposed to match every words tokenized by a dot, but not the last token: ^([^\.]+)(?:\.[^\.]+)?$|(?:([^\.]+\.[^\.]+)\.)
a => a
a.b => a
a.b.c => a.b
a.b.c.d => a.b.c
Upvotes: 0
Reputation: 784958
You can use this regex:
^([^.]+(?:\.[^.]+)?)
PS: Used ^([^.\n]+(?:\.[^.\n]+)?)
in regex101 demo since demo has multiple inputs in different lines.
Upvotes: 3
Reputation: 31290
Rather than a regex to capture the wanted part, eliminate the unwanted part:
s = s.replaceAll("\\.[^.]+$","");
Upvotes: 0
Reputation: 174696
Try this non-greedy regex.
(.+?)(?:\.[^.]*)?$
In java you need to escape the backslash one more time, so it would be like,
Pattern p = Pattern.compile("(.+?)(?:\\.[^.]*)?$");
Upvotes: 2