Difference between revisions of "Regexp/Cheatsheet"
Jump to navigation
Jump to search
(Created page with "{{DISPLAYTITLE:Regular expression cheatsheet}} This is small collection of regex that patterns I want to document because I think they are useful, at least for me. == Check...") |
|||
Line 6: | Line 6: | ||
For a [http://serverspec.org/ serverspec] test I needed to write a check where all unknown shell aliases should match in one query without looping. This is what I came up with. I know that the 3 <tt>grep</tt> patterns could be further reduced. |
For a [http://serverspec.org/ serverspec] test I needed to write a check where all unknown shell aliases should match in one query without looping. This is what I came up with. I know that the 3 <tt>grep</tt> patterns could be further reduced. |
||
^alias\s+(?!(?:egrep|fgrep|grep|which)=) |
/^alias\s+(?!(?:egrep|fgrep|grep|which)=)/ |
||
Which matches only the last 2 highlighted entries but not the leading 4 ones: |
Which matches only the last 2 highlighted entries but not the leading 4 ones: |
||
alias egrep= |
alias egrep= |
||
Line 14: | Line 14: | ||
<span class="highlight">alias whatever=</span> |
<span class="highlight">alias whatever=</span> |
||
<span class="highlight">alias foobar=</span> |
<span class="highlight">alias foobar=</span> |
||
== Match one or more keywords == |
|||
When writing an parser for the <tt>ip addr</tt> command I ran into a case where one or more flags had to be gotten before mangling them into an ruby array. The actual code is more complex but for brevity only the relevant part is shown. |
|||
/inet.*global\s(?:((?:(?:temporary|permanent|dynamic|secondary|primary|tentative|deprecated)\s+)+))?/ |
|||
The highlighted matches below would be recalled by <tt>$1</tt> |
|||
inet 10.0.0.1/24 scope global eth0-foo |
|||
inet 10.0.0.2/24 scope global <span class="highlight">secondary</span> eth0-foo |
|||
inet6 2001:db8::16b4/64 scope global <span class="highlight">temporary dynamic</span> |
|||
inet6 2001:db8::16b3/64 scope global <span class="highlight">deprecated dynamic</span> |
|||
inet6 2001:db8::16b2/64 scope global <span class="highlight">dynamic</span> |
Revision as of 00:24, 25 November 2015
This is small collection of regex that patterns I want to document because I think they are useful, at least for me.
Check unknown values only
For a serverspec test I needed to write a check where all unknown shell aliases should match in one query without looping. This is what I came up with. I know that the 3 grep patterns could be further reduced.
/^alias\s+(?!(?:egrep|fgrep|grep|which)=)/
Which matches only the last 2 highlighted entries but not the leading 4 ones:
alias egrep= alias grep= alias fgrep= alias which= alias whatever= alias foobar=
Match one or more keywords
When writing an parser for the ip addr command I ran into a case where one or more flags had to be gotten before mangling them into an ruby array. The actual code is more complex but for brevity only the relevant part is shown.
/inet.*global\s(?:((?:(?:temporary|permanent|dynamic|secondary|primary|tentative|deprecated)\s+)+))?/
The highlighted matches below would be recalled by $1
inet 10.0.0.1/24 scope global eth0-foo inet 10.0.0.2/24 scope global secondary eth0-foo inet6 2001:db8::16b4/64 scope global temporary dynamic inet6 2001:db8::16b3/64 scope global deprecated dynamic inet6 2001:db8::16b2/64 scope global dynamic