Note that there are some explanatory texts on larger screens.

plurals
  1. POCan sed regex simulate lookbehind and lookahead?
    primarykey
    data
    text
    <p>I'm trying to write a sed script that will capture all "naked" URL's in a text file and replace them with <code>&lt;a href=[URL]&gt;[URL]&lt;/a&gt;</code>. By "naked" I mean a URL that is not wrapped inside an anchor tag. </p> <p>My initial thought was that I should match URL's that do not have a " or a > in front of them, and also do not have a &lt; or a " after them. However, I am running into difficulty with expressing the concept of "do not have in front of or behind" because as far as I know sed does not have look-ahead or look-behind.</p> <p>Sample Input:</p> <pre><code>[Beginning of File]http://foo.bar arbitrary text http://test.com other text &lt;a href="http://foobar.com"&gt;http://foobar.com&lt;/a&gt; Nearing end of file!!! http://yahoo.com[End of File] </code></pre> <p>Sample Desired Output:</p> <pre><code>[Beginning of File]&lt;a href="http://foo.bar"&gt;http://foo.bar&lt;/a&gt; arbitrary text &lt;a href="http://test.com"&gt;http://test.com&lt;/a&gt; other text &lt;a href="http://foo.bar"&gt;http://foo.bar&lt;/a&gt; Nearing end of file!!! &lt;a href="http://yahoo.com"&gt;http://yahoo.com&lt;/a&gt;[End of File] </code></pre> <p>Observe that the third line is unmodified, because it is already inside <code>&lt;a href&gt;</code>. On the other hand, both the first and second lines are modified. Finally, observe that all non-URL text is unmodified.</p> <p>Ultimately, I am trying to do something like:</p> <pre><code>sed s/[^&gt;"](http:\/\/[^\s]\+)/&lt;a href="\1"&gt;\1&lt;\/a&gt;/g 2-7-2013 </code></pre> <p>I began by verifying that the following will correctly match and remove a URL:</p> <pre><code>sed 's/http:\/\/[^\s]\+//g' </code></pre> <p>I then tried this, but it is not able to match URL's that start at the beginning of file / input:</p> <pre><code>sed 's/[^\&gt;"]http:\/\/[^\s]\+//g' </code></pre> <p>Is there a way to work around this in sed, either by simulating lookbehind / lookahead, or explicitly matching beginning of file and end of file?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload