Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从字符串中删除页码_C#_Regex - Fatal编程技术网

C# 从字符串中删除页码

C# 从字符串中删除页码,c#,regex,C#,Regex,因此,我从各种书籍中获得了索引文件,我的目标是从这些文件中提取关键字。下面是我观察到的测试用例 1,3-dichlorobenzene 3, 6 2,3,4,6-tetrachlorophenol 13 3-D Analyst 178 Alabama 1 ArcView 82, 161-170, 173-174, 178-179, 181, 185 3-D Analyst 178 Spatial Analyst 178, 185 NH3 48, 56

因此,我从各种书籍中获得了索引文件,我的目标是从这些文件中提取关键字。下面是我观察到的测试用例

1,3-dichlorobenzene 3, 6 2,3,4,6-tetrachlorophenol 13 3-D Analyst 178 Alabama 1 ArcView 82, 161-170, 173-174, 178-179, 181, 185 3-D Analyst 178 Spatial Analyst 178, 185 NH3 48, 56 see also ammonia aluminum sulfate, Al2(SO4)3 159 calibration 32, 50-51, 58, 78, 84-86, 88, 103, 116, 118123, 139, 141, 144-155, 208, 221-222, 226-227, 233, 236, 243, 257, 259-261, channel 1-3, 99, 100, 102-103, 106, 120, 144, 208-209, 220, 222, 228, 236- 239, 246, 275, 295, 18, 320-328, 331, 337, 341 然而,我的第二个正则表达式也会弄乱我的数据,这就是我在替换后得到的结果

1,3-dichlorobenzene 2,3,4,6-tetrachlorophenol 3-D Analyst Alabama ArcView **-D Analyst** Spatial Analyst NH3 see also ammonia aluminum sulfate, Al2(SO4)3 calibration channel 这无法从第一个测试用例中删除数字,并返回“1,3-二氯苯3”

这使“Alabama 1”测试用例失败,并返回“Alabama 1”


我想我已经接近解决这个问题了,但我不确定我错过了什么。任何帮助都将不胜感激。

如果我正确理解您的问题,这应该可以

numbers = new Regex(@"(\s+\d{1,9},{1,9}[^a-z\-]+,?)", RegexOptions.IgnoreCase);

你可能喜欢也可能不喜欢我对这个问题的解决方案;但是看起来你的页码前面总是有一个双空格。因此,要删除页码,只需先按行分解,然后按双空格分解,并取数组的第一个元素,如下所示:

以下是我为您编写的一些快速而肮脏的代码:

$str = '1,3-dichlorobenzene  3, 6
2,3,4,6-tetrachlorophenol  13
3-D Analyst  178
Alabama 1
ArcView  82, 161-170, 173-174, 178-179, 181, 185
    3-D Analyst  178
    Spatial Analyst  178, 185
NH3  48, 56                     see also ammonia
aluminum sulfate, Al2(SO4)3   159
calibration  32,  50-51, 58, 78, 84-86, 88, 103,  116, 118123, 139,   141, 144-155,  208, 221-222, 226-227,  233,  236,  243, 257,  259-261, 
channel  1-3, 99, 100, 102-103, 106, 120, 144, 208-209, 220, 222, 228, 236-         239, 246, 275, 295, 18, 320-328, 331, 337, 341';

$str = str_replace('    ', '', $str);

$arr = explode("\r\n", $str);
//print_r($arr);
$final = array();

// phase 1

foreach ($arr as $item)
{
    $_arr = explode('  ', $item);
    $final[] = $_arr[0];
}

echo '<pre>';
print_r($final);
$final2 = array();

// phase 2
foreach ($final as $item)
{
    $final2[] = preg_replace('/[0-9](?![A-Z])\,*\-?/', '', $item);
}

//print_r($final2);

您将看到所有数字、破折号和逗号都被删除,只留下标题:

如果您单独处理每一行,那么下面的正则表达式就可以了:

string output = Regex.Replace(input, @"(?<!^[\d\s,]*)(?<!\w|\))\d+\s*(-\s*\d+)?,?", string.Empty);
(?


string output=Regex.Replace(input,@)(?我试过了,但没用,下面是输出-2,3,4,6-四氯苯酚13-3-D分析师178,我想这是因为最后一个数字没有“,“后面是另一个数字。我认为大多数情况下你是对的。我确实注意到文件中有几个奇怪的情况,它们后面没有双空格,因为数据是“脏的”很难编写程序来解决每个场景。在运行之前,您可能需要先将其清理干净。感谢您的努力,不幸的是,它确实弄乱了数据。请检查您答案中的硫酸铝公式,我将取消“另请参见XXXX”我重新阅读了你的问题。我的编辑如何?这与你在原始问题中要求的内容非常吻合。一旦我将其拆分,我将释放“请参见XXXX”,这是我希望避免拆分的主要原因。另外,在你的第二个回答中,你会注意到它缺少“3D Analyst”和“Spatial Analyst”,我无法从索引中跳过单词:)这太接近了。它不适用于“NH3 48,56\t\t\t\t\t也适用于氨”和“硫酸铝,Al2(SO4)”更改化学式是一个大问题:)很酷。我不知道这个问题是用C#写的。我以为是用PHP写的…@UlugbekUmirov你能告诉我你是如何为正则表达式生成图像的吗?你有没有使用任何工具来排除正则表达式的故障?你看起来像正则表达式方面的专家:)@user1477388因为@dcastro从我的问题标题中删除了C#:)@ndd user1477388已经提到了,它是调试和图像生成的有用工具。
numbers = new Regex(@"(\s+\d{1,9}[^a-z\-]+,?)", RegexOptions.IgnoreCase);
numbers = new Regex(@"(\s+\d{1,9},{1,9}[^a-z\-]+,?)", RegexOptions.IgnoreCase);
$str = '1,3-dichlorobenzene  3, 6
2,3,4,6-tetrachlorophenol  13
3-D Analyst  178
Alabama 1
ArcView  82, 161-170, 173-174, 178-179, 181, 185
    3-D Analyst  178
    Spatial Analyst  178, 185
NH3  48, 56                     see also ammonia
aluminum sulfate, Al2(SO4)3   159
calibration  32,  50-51, 58, 78, 84-86, 88, 103,  116, 118123, 139,   141, 144-155,  208, 221-222, 226-227,  233,  236,  243, 257,  259-261, 
channel  1-3, 99, 100, 102-103, 106, 120, 144, 208-209, 220, 222, 228, 236-         239, 246, 275, 295, 18, 320-328, 331, 337, 341';

$str = str_replace('    ', '', $str);

$arr = explode("\r\n", $str);
//print_r($arr);
$final = array();

// phase 1

foreach ($arr as $item)
{
    $_arr = explode('  ', $item);
    $final[] = $_arr[0];
}

echo '<pre>';
print_r($final);
$final2 = array();

// phase 2
foreach ($final as $item)
{
    $final2[] = preg_replace('/[0-9](?![A-Z])\,*\-?/', '', $item);
}

//print_r($final2);
Array
(
    [0] => 1,3-dichlorobenzene
    [1] => 2,3,4,6-tetrachlorophenol
    [2] => 3-D Analyst
    [3] => Alabama 1
    [4] => ArcView
    [5] => 3-D Analyst
    [6] => Spatial Analyst
    [7] => NH3
    [8] => aluminum sulfate, Al2(SO4)3
    [9] => calibration
    [10] => channel
)
(?<!^[\d\s,]*)(?<!\w|\))\d+\s*(-\s*\d+)?,?
string output = Regex.Replace(input, @"(?<!^[\d\s,]*)(?<!\w|\))\d+\s*(-\s*\d+)?,?", string.Empty);