提取字符之间的数字-grep


extract number between characters - grep

我使用的是PHP,希望使用grep提取字符'gi|HERE IS THE NUMBER|'之间的数字,并将其分配给变量。

该文件包含:

>gi|1786181|gb|AE000111|ECAE000111 Escherichia coli , thrL, thrA, thrB, thrC, yaaA, yaaJ, talB, mog, yaaH genes from bases 1 to 10596
>gi|1786192|gb|AE000112|ECAE000112 Escherichia coli , htgA, yaaI, dnaK, dnaJ, yi81_1, yi82_1

我想在这里提取数字:gi|1786181|gi|1786192|,并将它们放在x和y变量中。

因此,输出将为:x=1786181y=1786192

我尝试:

$x = shell_exec("C:''cygwin64''bin''bash.exe --login -c '" grep -o 'gi'|[0-9]'|'  $file.txt >  $result.txt 2>&1'"");    

它不起作用。请帮忙吗?

为什么要通过shell使用grep?使用preg_match():

$input = file_get_contents('file.txt');
preg_match("/gi'|([0-9]+)'|/", $input, $matches);

$matches将返回如下内容,数组中的第一项包含完全匹配,第二项仅包含您要查找的值:

array(
    0 => array(
        "gi|1786181|",
        "gi|1786192|"
    ),
    1 => array(
        "1786181",
        "1786192"
    )
)

在任何一种情况下,您都需要指定需要一个或多个整数:

gi'|([0-9]+)'|
$str = 'gi|1786181|gb|AE000111|ECAE000111 Escherichia coli , thrL, thrA, thrB, thrC, yaaA, yaaJ, talB, mog, yaaH genes from bases 1 to 10596';
preg_match("/'|([0-9]+)'|/", $str, $matches);
print_r($matches);