Jatin
Jatin

Reputation: 31724

Ignore strings after character if character found

I need to have regex which finds the child and parent. For example:

-->2014-05-21 22:05:23.092 INFO [Core] org.jboss.modules.ModuleClassLoader@692578be parents: sun.misc.Launcher$AppClassLoader@62c8aeb3, sun.misc.Launcher$ExtClassLoader@65459c6f

The child is org.jboss.modules.ModuleClassLoader@692578be and its parent is sun.misc.Launcher$AppClassLoader@62c8aeb3. The later string after , is ignored.

-->2014-05-21 22:04:34.118 INFO [Core] sun.misc.Launcher$AppClassLoader@62c8aeb3 parent: sun.misc.Launcher$ExtClassLoader@65459c6f

In above the child is sun.misc.Launcher$AppClassLoader@62c8aeb3 and the parent is sun.misc.Launcher$ExtClassLoader@65459c6f

I have this regular expression .* (.*) parents?: (.*),?.*. It works with the second example. But for the first one outputs parent as:

sun.misc.Launcher$AppClassLoader@62c8aeb3, sun.misc.Launcher$ExtClassLoader@65459c6f

but instead it should: sun.misc.Launcher$AppClassLoader@62c8aeb3

Upvotes: 0

Views: 69

Answers (2)

anubhava
anubhava

Reputation: 784968

You can use this regex:

(\S+) parents?: ([^,]+)

Working Demo

Upvotes: 4

Uri Agassi
Uri Agassi

Reputation: 37409

Regex is greedy by default, you should tell it to exclude ,:

.* (.*) parents?: ([^,]*),?.*

Upvotes: 2

Related Questions