Note that there are some explanatory texts on larger screens.

plurals
  1. POunix: how to tell if a string matches a regex
    primarykey
    data
    text
    <p>Trying out <a href="http://fishshell.com/" rel="noreferrer">fish shell</a>, so I'm translating my bash functions. The problem is that in one case, I'm using bash regexes to check if a string matches a regex. I can't figure out how to translate this into fish.</p> <p><a href="https://github.com/JoshCheek/dotfiles/blob/d1662522/dotfiles/profile#L49" rel="noreferrer">Here</a> is my example.</p> <pre><code>if [[ "$arg" =~ ^[0-9]+$ ]] ... </code></pre> <ul> <li>I looked into <strong>sed</strong>, but I don't see a way to get it to set its exit status based on whether the regex matches.</li> <li>I looked into delegating to <strong>Ruby</strong>, but again, getting the exit status set based on the match requires making this really ugly (see below).</li> <li>I looked into delegating back to <strong>bash</strong>, but despite trying maybe three or four ways, never got that to match.</li> </ul> <p><strong>So, is there a way in *nix to check if a string matches a regex, so I can drop it into a conditional?</strong></p> <hr> <p>Here is what I have that currently works, but which I am unhappy with:</p> <pre><code># kill jobs by job number, or range of job numbers # example: k 1 2 5 # example: k 1..5 # example: k 1..5 7 10..15 # example: k 1-5 7 10-15 function k for arg in $argv if ruby -e "exit ('$arg' =~ /^[0-9]+\$/ ? 0 : 1)" kill -9 %$arg else set _start (echo "$arg" | sed 's/[^0-9].*$//') set _end (echo "$arg" | sed 's/^[0-9]*[^0-9]*//') for n in (seq $_start $_end) kill -9 %"$n" end end end end </code></pre>
    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.
    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