简体   繁体   中英

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();
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM