如何使用正则表达式验证内部版本号

时间:2018-07-18 01:09:44

标签: regex bash

我有一个格式为DGTS_2.11.2311231231的内部版本号,如果格式不匹配,我想验证输入和退出,我试图创建类似这样的东西,但是它不起作用,请帮助我

version=$1
version_checked=$(echo $version | sed -e 'DGTS_\/[0-9]\+\.[0-9]{1,2}\+\.[0-9]{1,10}/d')
version_checked=DGTS_*
if [[ -z $version_checked ]]; then
echo "$version is the right format"
else
 echo "$version_checked is not  in the right format, please use XX.XX.XX format (ie: DGTS_2.10.3)"
exit

如果输入格式错误,此代码段将无效

1 个答案:

答案 0 :(得分:1)

我假设前两个数字区域可以是一个或两个数字,而最后一个数字区域是1-10。您的程序逻辑和语法有几个问题,不适用于我删除的正则表达式本身:

import numpy as np
from numba import jit
from numba import float64
from numba import int64


@jit((float64[:], int64), nopython=True, nogil=True)
def _ewma(arr_in, window):
    r"""Exponentialy weighted moving average specified by a decay ``window``
    to provide better adjustments for small windows via:

        y[t] = (x[t] + (1-a)*x[t-1] + (1-a)^2*x[t-2] + ... + (1-a)^n*x[t-n]) /
               (1 + (1-a) + (1-a)^2 + ... + (1-a)^n).

    Parameters
    ----------
    arr_in : np.ndarray, float64
        A single dimenisional numpy array
    window : int64
        The decay window, or 'span'

    Returns
    -------
    np.ndarray
        The EWMA vector, same length / shape as ``arr_in``

    Examples
    --------
    >>> import pandas as pd
    >>> a = np.arange(5, dtype=float)
    >>> exp = pd.DataFrame(a).ewm(span=10, adjust=True).mean()
    >>> np.array_equal(_ewma_infinite_hist(a, 10), exp.values.ravel())
    True
    """
    n = arr_in.shape[0]
    ewma = np.empty(n, dtype=float64)
    alpha = 2 / float(window + 1)
    w = 1
    ewma_old = arr_in[0]
    ewma[0] = ewma_old
    for i in range(1, n):
        w += (1-alpha)**i
        ewma_old = ewma_old*(1-alpha) + arr_in[i]
        ewma[i] = ewma_old / w
    return ewma


@jit((float64[:], int64), nopython=True, nogil=True)
def _ewma_infinite_hist(arr_in, window):
    r"""Exponentialy weighted moving average specified by a decay ``window``
    assuming infinite history via the recursive form:

        (2) (i)  y[0] = x[0]; and
            (ii) y[t] = a*x[t] + (1-a)*y[t-1] for t>0.

    This method is less accurate that ``_ewma`` but
    much faster:

        In [1]: import numpy as np, bars
           ...: arr = np.random.random(100000)
           ...: %timeit bars._ewma(arr, 10)
           ...: %timeit bars._ewma_infinite_hist(arr, 10)
        3.74 ms ± 60.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
        262 µs ± 1.54 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

    Parameters
    ----------
    arr_in : np.ndarray, float64
        A single dimenisional numpy array
    window : int64
        The decay window, or 'span'

    Returns
    -------
    np.ndarray
        The EWMA vector, same length / shape as ``arr_in``

    Examples
    --------
    >>> import pandas as pd
    >>> a = np.arange(5, dtype=float)
    >>> exp = pd.DataFrame(a).ewm(span=10, adjust=False).mean()
    >>> np.array_equal(_ewma_infinite_hist(a, 10), exp.values.ravel())
    True
    """
    n = arr_in.shape[0]
    ewma = np.empty(n, dtype=float64)
    alpha = 2 / float(window + 1)
    ewma[0] = arr_in[0]
    for i in range(1, n):
        ewma[i] = arr_in[i] * alpha + ewma[i-1] * (1 - alpha)
    return ewma

根据您的请求,这是一个正则表达式,可与可选的第四象限配合使用,并允许#!/bin/bash version=$1 if [[ $version =~ ^DGTS_([0-9]{1,2}\.){2}[0-9]{1,10}$ ]]; then echo "$version is the right format" else echo "$version is not in the right format, please use XX.XX.XX format (ie: DGTS_2.10.3)" exit fi 通过:

DGTS_2.11.0.4661505