vaibhav
vaibhav

Reputation: 41

Regex Match till first occurrence of string

I'm trying to match starting from @Path("/v11/data") till next @GET.

This is a file and I'm using this expression \/v11\/data(\s?.*)+ but this turns greedy and doesn't stop at the first match of @GET.

When I use this expression \/v11\/data(\s?.*)+\@GET. Please help

DEMO LINK: https://regex101.com/r/BDsUvM/3

public class TestResource {
private final TestResource testResource;

public TestResource(TestResource testResource) {
    this.testResource = testResource;
}

@GET
@Path("/v11/data")
@Resource[enter(asdf)
public void getTest(@PathParam("asdf") @asdf String asdf,
                           @PathParam("dfg") dgf dgf,
                           @NotNull @SS ss sess) {

    testResource.getTest();
}

@GET
@Path("/v45/data")
public void getData(@PathParam("asdf") @asdf String asdf,
                           @PathParam("ewr") erw ewr,
                           @NotNull @ss ss ss) {

    testResource.getData();
}

@GET
@Path("/v45/data")
public void getData(@PathParam("asdf") @asdf String asdf,
                           @PathParam("ewr") erw ewr,
                           @NotNull @ss ss ss) {

    testResource.getData();
}

Upvotes: 1

Views: 72

Answers (1)

The fourth bird
The fourth bird

Reputation: 163577

You could match starting from @Path and match all the next lines that do not start with @GET using a negative lookahead in a repeating group.

@Path\("\/v11\/data.*(?:\r?\n(?!@GET).*)*

Regex demo

If @GET should be there, you could use a capturing group

@Path\("\/v11\/data.*((?:\r?\n(?!@GET).*)*)\r?\n\s*@GET

Regex demo

Edit

Or a shorter more readable version as suggested by @anubhava

Path\("/v11/data"\)\s+((?:.*\R)+?)@GET

Regex demo

Upvotes: 2

Related Questions