正则表达式通过在

时间:2017-02-27 20:11:37

标签: regex

我有这样的文字:

something text
(some text here image and more text)
some more text
(test)
text

我想对2个括号之间的所有内容进行正则表达式搜索并搜索单词image,如果该单词存在于2个括号之间,那么我想在该行之后添加一个新行。所以我的正则表达式应该产生这个输出:

something text
(some text here image and more text)

some more text
(test)
text

我怎样才能做到最好?我已经尝试了(?<=\()(?=image)(?=\))但是没有用。

3 个答案:

答案 0 :(得分:1)

使用单词边界:

\(.*\bimage\b.*\)

要在匹配时捕获该模式,请将其放在括号内:

(\(.*\bimage\b.*\))

然后尝试使用$1(或\1引用该组,具体取决于正在使用正则表达式的语言。)

答案 1 :(得分:1)

您可以使用以下正则表达式在括号中搜索单词image,并将其替换为捕获的组和新行,您可以获得预期结果:

(\(.*?image.*?\))

input        >>  something text
                 (some text here image and more text)
                 some more text
                 (test)
                 text
regex search >>  (\(.*?image.*?\))
replace with >>  `$1\n`
output       >>  something text
                 (some text here image and more text)

                 some more text
                 (test)
                 text

参见 demo / explanation

答案 2 :(得分:1)

您没有提及工具,但使用sed 's/(.*image.*)/&\n/' file

\b

如果你想限制独立单词“image”使用sed 's/(.*\bimage\b.*)/&\n/' file 单词边界(我认为只有GNU sed)

function RunWorkflow(in_entitiId,in_workflowId,in_url) { 
    var _return = window.confirm('Do you want to execute workflow ?');
    if (_return) {
        var url = in_url;
        var entityId =in_entitiId ;
        var workflowId = in_workflowId;
        var OrgServicePath = "/XRMServices/2011/Organization.svc/web";
        url = url  + OrgServicePath;
        var request;
        request = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
                    "<s:Body>" +
                        "<Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">" +
                        "<request i:type=\"b:ExecuteWorkflowRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">" +
                            "<a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">" +
                            "<a:KeyValuePairOfstringanyType>" +
                                "<c:key>EntityId</c:key>" +
                                "<c:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">" + entityId + "</c:value>" +
                            "</a:KeyValuePairOfstringanyType>" +
                            "<a:KeyValuePairOfstringanyType>" +
                                "<c:key>WorkflowId</c:key>" +
                                "<c:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">" + workflowId + "</c:value>" +
                            "</a:KeyValuePairOfstringanyType>" +
                            "</a:Parameters>" +
                            "<a:RequestId i:nil=\"true\" />" +
                            "<a:RequestName>ExecuteWorkflow</a:RequestName>" +
                        "</request>" +
                        "</Execute>" +
                    "</s:Body>" +
                    "</s:Envelope>";

        var req = new XMLHttpRequest();
        req.open("POST", url, true)
        // Responses will return XML. It isn't possible to return JSON.
        req.setRequestHeader("Accept", "application/xml, text/xml, */*");
        req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
        req.onerror = displayError;
        req.onreadystatechange = function () { assignResponse(req); };
        req.send(request);
    }
    function displayError(e) {
    alert(this.status);
    }

}

function assignResponse(req) {
    if (req.readyState == 4) {
        if (req.status == 200) {
            alert('successfully executed the workflow');
        }
    }
}