1

Let me preface this with I am pretty noob at doing regex. The code I am writing is for macOS, not iOS but I don't think that matters in this case. I'm trying to search a git diff return based on the change range indicators. Here's is the content I am searching (the variable is called element):

3b8d5178f4d21e9269547e8f0bae4b7daa7d8206
Author: Steve <[email protected]>
Date:   Fri Oct 4 16:55:50 2019 -0400

    Fixed responsiveness of code blocks

diff --git a/examples/video/outstream/pb-ve-outstream-no-server.html b/examples/video/outstream/pb-ve-outstream-no-server.html
index ae813578..e18e31ec 100644
--- a/examples/video/outstream/pb-ve-outstream-no-server.html
+++ b/examples/video/outstream/pb-ve-outstream-no-server.html
@@ -24,7 +24,7 @@ sidebarType: 4

        <div>

-           <div>
+           <div style="width:60vw;">

                <p>Lorem ipsum dolor sit ....</p>
            </div>
@@ -33,7 +33,7 @@ sidebarType: 4
                <p>Prebid Outstream Video Ad</p>
             </div>

-           <div>
+           <div style="width:60vw;">
                <p>Sed ut perspiciatis ...</p>
             </div>
        </div>          
@@ -49,9 +49,9 @@ sidebarType: 4
            Do not forget to exchange the placementId in the code examples with your own placementId!</p>
        </div>

-       <div>
+       <div style="width:60vw;">
            <h4>Place this code in the page header.</h4>
-<pre class="pb-code-hl">
+<pre class="pb-code-hl" style="width:60vw;">
 <!--put javascript code here-->
 &lt;script&gt;
     var pbjs = pbjs || {};
@@ -95,9 +95,9 @@ sidebarType: 4
        </div>

        <!--body code example-->
-       <div>
+       <div style="width:60vw;">
            <h4>Place this code in the page body.</h4>
-<pre class="pb-code-hl">
+<pre class="pb-code-hl" style="width:60vw;">
 <!--put body html and javascript here-->
 &lt;div id='video1'&gt;
    &lt;p&gt;Prebid Outstream Video Ad&lt;/p&gt;

here's my regex to get the change indicators

 do {
     let regex = try NSRegularExpression(pattern: "@@ (.*) @@", options: .caseInsensitive)
     let matches = regex.matches(in: element, options:[], range: NSRange(location: 0, length: element.utf16.count))

     for match in matches {
         let strElement = element as NSString
         arrMatches.append(strElement.substring(with: match.range) as String)

     }
 } catch {
     print ("Error with regex return")
 }

Works as expected, I get all four instances:

["@@ -24,7 +24,7 @@", "@@ -33,7 +33,7 @@", "@@ -49,9 +49,9 @@", "@@ -95,9 +95,9 @@"]

I now want to capture the changes between the indicators. But I am not getting any results, not even an error on the catch. Here's a hard coded example of what I am trying to do:

do {
   let regex = try NSRegularExpression(pattern: "@@ -24,7 +24,7 @@ (.*) @@ -33,7 +33,7 @@", options: .caseInsensitive)
   let matches = regex.matches(in: element, options:[], range: NSRange(location: 0, length: element.utf16.count))
   print(matches.count)//<--prints 0, expecting 1                  
   for match in matches {
      let strElement = element as NSString
      print(":::" + strElement.substring(with: match.range) as String) //<--Nothing prints
   }
 } catch {
      print ("Error with regex return")//<-but nothing prints here either...
 }

The content has a lot of tabs and linebreaks so I am wondering if I somehow have to account for those within my regex? Any help would be appreciated.

I should get 4 results, the first result should be:

sidebarType: 4

        <div>

-           <div>
+           <div style="width:60vw;">

                <p>Lorem ipsum dolor sit ....</p>
            </div>

the second result would be content between change indicator 2 and 3, the third content between 3 and 4 and the last all content after 4

5
  • If you plan to get some text between the two substrings, @@ -24,7 +24,7 @@ and @@ -33,7 +33,7 @@, use "(?s)@@ -24,7 \\+24,7 @@(.*)@@ -33,7 \\+33,7 @@" Commented Nov 19, 2019 at 14:05
  • See the edits I added. Commented Nov 19, 2019 at 14:06
  • @WiktorStribiżew - That was it. Thank you. Make it an answer and I'll accept it! Commented Nov 19, 2019 at 14:10
  • I posted a dynamic regex solution. Commented Nov 19, 2019 at 14:17
  • "@@ -24,7 \\+24,7 @@([\\S\\s]*?)@@ -33,7 \\+33,7 @@" Commented Nov 19, 2019 at 14:22

1 Answer 1

1

You may use

(?s)@@(?:\s+[-+]?\d+(?:,\d+)?){2}\s+@@(.*?)(?=@@(?:\s+[-+]?\d+(?:,\d+)?){2}\s+@@|\z)

Get the Group 1 value using strElement.substring(with: match.range(at: 1)) as String).

See the regex demo

See the Swift code demo:

let element="3b8d5178f4d21e9269547e8f0bae4b7daa7d8206\nAuthor: Steve <[email protected]>\nDate:   Fri Oct 4 16:55:50 2019 -0400\n\n    Fixed responsiveness of code blocks\n\ndiff --git a/examples/video/outstream/pb-ve-outstream-no-server.html b/examples/video/outstream/pb-ve-outstream-no-server.html\nindex ae813578..e18e31ec 100644\n--- a/examples/video/outstream/pb-ve-outstream-no-server.html\n+++ b/examples/video/outstream/pb-ve-outstream-no-server.html\n@@ -24,7 +24,7 @@ sidebarType: 4\n\n        <div>\n\n-           <div>\n+           <div style=\"width:60vw;\">\n\n                <p>Lorem ipsum dolor sit ....</p>\n            </div>\n@@ -33,7 +33,7 @@ sidebarType: 4\n                <p>Prebid Outstream Video Ad</p>\n             </div>\n\n-           <div>\n+           <div style=\"width:60vw;\">\n                <p>Sed ut perspiciatis ...</p>\n             </div>\n        </div>          \n@@ -49,9 +49,9 @@ sidebarType: 4\n            Do not forget to exchange the placementId in the code examples with your own placementId!</p>\n        </div>\n\n-       <div>\n+       <div style=\"width:60vw;\">\n            <h4>Place this code in the page header.</h4>\n-<pre class=\"pb-code-hl\">\n+<pre class=\"pb-code-hl\" style=\"width:60vw;\">\n <!--put javascript code here-->\n &lt;script&gt;\n     var pbjs = pbjs || {};\n@@ -95,9 +95,9 @@ sidebarType: 4\n        </div>\n\n        <!--body code example-->\n-       <div>\n+       <div style=\"width:60vw;\">\n            <h4>Place this code in the page body.</h4>\n-<pre class=\"pb-code-hl\">\n+<pre class=\"pb-code-hl\" style=\"width:60vw;\">\n <!--put body html and javascript here-->\n &lt;div id='video1'&gt;\n    &lt;p&gt;Prebid Outstream Video Ad&lt;/p&gt;"
do {
   let del = "@@(?:\\s+[-+]?\\d+(?:,\\d+)?){2}\\s+@@"
   let regex = try NSRegularExpression(pattern: "(?s)\(del)(.*?)(?=\(del)|\\z)", options: .caseInsensitive)
   let matches = regex.matches(in: element, options:[], range: NSRange(location: 0, length: element.utf16.count))
   print(matches.count)//<--prints 0, expecting 1                  
   for match in matches {
      let strElement = element as NSString
      print(":::" + strElement.substring(with: match.range(at: 1)) as String) //<--Nothing prints
   }
 } catch {
      print ("Error with regex return")//<-but nothing prints here either...
 }

The regex is of the (?s)\(del)(.*?)(?=\(del)|\z) form:

  • (?s) - a DOTALL inline regex modifier
  • \(del) - the delimiter pattern
  • (.*?) - Group 1: any 0+ chars, as few as possible
  • (?=\(del)|\z) - right after, the must be a delimiter pattern or (|) end of string (\z).

The delimiter pattern matches

  • @@ - a literal string
  • (?:\s+[-+]?\d+(?:,\d+)?){2} - two occurrences of
    • \s+ - 1+ whitespaces
    • [-+]? - an optional + or -
    • \d+ - 1+ digits
    • (?:,\d+)? - an optional sequence of , and 1+ digits
  • \s+ - 1+ whitespaces
  • @@ - a literal string
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.