grep:重复操作符操作数无效

时间:2013-03-21 02:34:00

标签: grep ack

我有这个常规快递(?<=heads\/)(.*?)(?=\n),你可以看到它在这里工作 http://regexr.com?347dm

我需要这个正则表达式才能在grep命令中工作,但我收到了这个错误。

$ grep -Eio '(?<=heads\/)(.*?)(?=\n)' text.txt 
grep: repetition-operator operand invalid

它在ack中效果很好,但我在机器上没有确认需要运行它。

ack text.txt -o --match '(?<=heads\/)(.*?)(?=\n)'

的text.txt

74f3649af36984e1b784e46502fe318e91d29570    HEAD
06d4463ab47a6246e6bd94dc3b9267d59fc16c2e    refs/heads/ARC
0597e13c22b6397a1b260951f9d064f668b26f08    refs/heads/LocationAge
e7e1ed942d15efb387c878b9d0335b37560c8807    refs/heads/feature/311-312-breaking-banner-updates
d0b2632b465702d840a358d0b192198ae505011c    refs/heads/gulf-news
509173eafc6792739787787de0d23b0c804d4593    refs/heads/jbb-new-applicationdidfinishlaunching
1e7b03ce75b1a7ba47ff4fb5128bc0bf43a7393b    refs/heads/locationdebug
74f3649af36984e1b784e46502fe318e91d29570    refs/heads/master
5d2ede384325877c24db7ba1ba0338dc7b7f84fb    refs/heads/mixed-media
3f3b6a81dd3baea8744aec6b95c2fe4aaeb20ea3    refs/heads/post-onezero
4198a43aab2dfe72d7ae9e9e53fbb401fc9dac1f    refs/heads/whitelabel
76741013b3b2200de29f53800d51dfd6dc7bac5e    refs/tags/r10
fc53b1a05dad3072614fb397a228819a67615b82    refs/tags/r10^{}
afdcfd970c9387f6fda0390ef781c2776aa666c3    refs/tags/r11

3 个答案:

答案 0 :(得分:12)

grep不支持(?<=...)*?(?=...)运营商。请参阅this table

答案 1 :(得分:5)

$ grep -Pio '(?<=heads\/)(.*?)(?=\n)' text.txt # P option instead of E

如果您使用GNU grep,则可以使用-P--perl-regexp选项。

如果您使用的是OS X,则需要安装GNU grep。

$ brew install grep 

由于最近changes,要在macOS上使用GNU grep,你必须在命令前加上'g'

$ ggrep -Pio '(?<=heads\/)(.*?)(?=\n)' text.txt # P option instead of E

或更改path name

答案 2 :(得分:0)

尝试一下

import java.util.Collections;
import java.util.concurrent.CompletableFuture;

import com.microsoft.aad.msal4j.ClientCredentialFactory;
import com.microsoft.aad.msal4j.ClientCredentialParameters;
import com.microsoft.aad.msal4j.ConfidentialClientApplication;
import com.microsoft.aad.msal4j.IAuthenticationResult;

public class AADClientCred {
    public static void main(String[] args) {

        String tenantID = "<your tenant ID or name>";
        String clientID = "<your Azure AD app id>";
        String Secret = "<your azure ad app secret>";
        String authority = "https://login.microsoftonline.com/" + tenantID;
        //I use microsoft graph as resource for demo
        String scope = "https://graph.microsoft.com/.default";

        try {
            String access_token = getAccessTokenByClientCredentialGrant(clientID, Secret, authority, scope)
                    .accessToken();

            System.out.println("access token : " + access_token);

        } catch (Exception e) {

            e.printStackTrace();
        }

    }

    private static IAuthenticationResult getAccessTokenByClientCredentialGrant(String clientID, String Secret,
            String authority, String scope) throws Exception {

        ConfidentialClientApplication app = ConfidentialClientApplication
                .builder(clientID, ClientCredentialFactory.createFromSecret(Secret)).authority(authority).build();

        ClientCredentialParameters clientCredentialParam = ClientCredentialParameters
                .builder(Collections.singleton(scope)).build();

        CompletableFuture<IAuthenticationResult> future = app.acquireToken(clientCredentialParam);
        return future.get();
    }

}