简体   繁体   English

如何使用正则表达式在两个分隔符之间的字符串中 output 的第二个值?

[英]How to output the second value in the string between the two delimiters using regex?

I am fetching data from a text file.我正在从文本文件中获取数据。 How can I fetch the value in between the two delimiters ie |如何获取两个分隔符之间的值,即 | based on the match.根据比赛。 The first 7 characters are part of the id.前 7 个字符是 id 的一部分。

Input Example:输入示例:

    0123456|BHKAHHHHkk|12345678|JuiKKK121255
    9100450|HHkk|12348888|JuiKKK10000000021sdadad255

Desired Output: BHKAHHHHkk based on the searchfor value = 0123456所需的searchfor : BHKAHHHHkk基于搜索值 = 0123456

My Script:我的脚本:

$file = 'file.txt';


// the following line prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');

// get the file contents, assuming the file to be readable (and exist)
$contents = file_get_contents($file);
// escape special characters in the query
$txt = explode("\n",$contents);

$counter = 0;
foreach($txt as $key => $line){
    $subbedString = substr($line,2,6);

   // $searchfor = '0123456';
    //echo strpos($subbedString,$searchfor); 
    if(strpos($subbedString,$searchfor) === 0){
        $matches[$key] = $searchfor;
        
          echo  "<p>" . $matches[$key] . "</p>";
          
                  $counter += 1;
                  if($counter==10) break;
         
    }

preg_match function can be used for this purpose and it makes your code very simple and readable. preg_match function 可用于此目的,它使您的代码非常简单易读。

Each row of data starts with \n or start of the content, so \n|^ for the beginning of the pattern is used;每行数据都以\n或内容开头,所以使用\n|^作为模式的开头; Also ?: is used to prevent listing this part in the $matchs variable.另外?:用于防止在$matchs变量中列出此部分。

Each row of data ends with \n or end of content, so \n|$ for the end of the pattern is used;每行数据以\n或内容结尾结束,所以使用\n|$作为模式的结尾; \r may appear or not so (?:\r?\n|$) used at the end. \r可能出现也可能不出现,最后使用(?:\r?\n|$)

$searchfor variable can be directly in the pattern. $searchfor变量可以直接在模式中。

\|([^\n]+) searchs for delimiters and rest of line data. \|([^\n]+)搜索行数据的分隔符和 rest。

In the end, the second parameter of the data (which you looking for) can be accessed in the $matchs[1] variable and the rest of the data in the $matchs[2] variable.最后,可以在$matchs[1]变量和$matchs[2]变量中的数据的 rest 中访问数据的第二个参数(您要查找的)。

<?php

$contents = '0123456|BHKAHHHHkk|12345678|JuiKKK121255
9100450|HHkk|12348888|JuiKKK10000000021sdadad255';

$searchfor = '9100450';

if(preg_match('/(?:\n|^)' . $searchfor .  '\|([^|]+)(.*?)(?:\r?\n|$)/is', $contents, $matchs))
{
    $second_value = $matchs[1];
    $reset_values = $matchs[2];
    echo "Result for searching <b>$searchfor</b> is <b>$second_value</b> and reset of data is <b>$reset_values</b>";
}
else echo('Not Found :(');

?>

Using regular expressions works for me:使用正则表达式对我有用:

$input = '0123456|BHKAHHHHkk|12345678|JuiKKK121255
9100450|HHkk|12348888|JuiKKK10000000021sdadad255';

$searchfor = '12348888';

$regexp = "/(?<=" . $searchfor . "\\|)\\w+/m";

$result = preg_match_all($regexp, $input, $matches);

print_r($matches);

Output: Output:

/*Array
(
    [0] => Array
        (
            [0] => JuiKKK10000000021sdadad255
        )

)*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM