Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Regular expressions are a method of describing regular grammars. They accomplish this in a very concise and very efficient way. This makes them look complicated.</p> <p>They are also structured and decodeable.</p> <p>First, there is a <code>sed</code> call.</p> <pre><code>sed "{operation}/{expression}/{replacement}/{modifiers}" {argument} </code></pre> <p>Note </p> <ul> <li>that <code>sed</code> separates the parts with forward slashes. This means you cannot have an unescaped forward slash in <code>{expression}</code> or <code>{replacement}</code>.</li> <li>that unlike most other regex dialets, <code>sed</code> uses parentheses to match actual parentheses and <em>escaped</em> parentheses to define capturing groups.</li> </ul> <p>The <code>{operation}</code> happens to be <code>s</code> - substitute. </p> <p>The <code>{expression}</code> is <code>\(^[a-z,0-9]\)\(.*\)\( [a-z,0-9]*$\)</code>, which breaks down as</p> <pre> \( # start capture group 1 ^ # match the start of the string [a-z,0-9] # match characters a-z and 0-9 and a comma (!) \) # end capture group 1 \( # start capture group 2 .* # match any character (.), zero or more times (*) \) # end capture group 2 \( # start capture group 3 # match a space [a-z,0-9]* # match characters a-z and 0-9 and a comma (!) $ # match the end of the string \) # end capture group 3 </pre> <p>Just think for a second, how much code (and time) it would take you to write a function that does the same, and how little space a regex needs. That's why it's harder to read - it's extremely compressed.</p> <p>The <code>{replacement}</code> is <code>\1\2 \1</code>. <code>\n</code> is called a back-reference, where <code>n</code> is the number ofthe capturing group. So this inserts the contents of groups 1 and 2, a space and the contents of group 1 again.</p> <p>The <code>{modifiers}</code> part is a <code>g</code> flag, that makes the regex apply as often as possible. In this special case it doesn't make much sense since the regex above can only ever match once anyway.</p>
 

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