正则表达式在连字符前后获取文本
Regex get text before and after a hyphen
我有这个字符串:
"Common Waxbill - Estrilda astrild"
如何为连字符前后的单词编写 2 个单独的正则表达式?我想要的输出是:
"Common Waxbill"
和
"Estrilda astrild"
这很简单:
.*(?= - ) # matches everything before " - "
(?<= - ).* # matches everything after " - "
请参阅 lookaround assertions 上的教程。
如果您不能使用回顾,但您的字符串始终采用相同的格式并且不能包含多个连字符,您可以使用
^[^-]*[^ -]
表示第一个,\w[^-]*$
表示第二个(或者 [^ -][^-]*$
如果连字符后的第一个非 space 不一定是单词-字符.
一点解释:
^[^-]*[^ -]
匹配字符串的开头(锚点 ^
),后跟任意数量的不是连字符的字符,最后是不是连字符或 space 的字符(只是为了排除最后 space 来自比赛)。
[^ -][^-]*$
采用相同的方法,但反过来,首先匹配既不是 space 也不是连字符的字符,然后是任意数量的字符,不是连字符,最后是结尾字符串(锚 $
)。 \w[^-]*$
基本相同,它使用更严格的 \w
而不是 [^ -]
。这再次用于从匹配中排除连字符后的 whitespace。
另一个解决方案是在连字符上拆分字符串并删除白色 space。
两种替代方法
你的问题的主要挑战是你想要两个单独的项目。这意味着您的流程依赖于另一种语言。 RegEx 本身不解析或分隔字符串;它仅解释了我们正在寻找的内容。 您使用的语言将进行实际分离。我的答案在 PHP 中得到了你的结果,但其他语言应该有类似的解决方案。
如果您只想完成问题中的工作,并且如果您正在使用 PHP...
方法一:explode("-", $list);
-> $array[]
如果您的列表超过两项,这将很有用:
<?php
// Generate our list
$list = "Common Waxbill - Estrilda astrild";
$item_arr = explode("-", $list);
// Iterate each
foreach($item_arr as $item) {
echo $item.'<br>';
}
// See what we have
echo '
<pre>Access array directly:</pre>'.
'<pre>'.$item_arr[0].'x <--notice the trailing space</pre>'.
'<pre>'.$item_arr[1].' <--notice the preceding space</pre>';
...您可以清理每个项目并使用 trim()
将它们重新分配给新数组。这将得到您的问题要求的文本(前后没有额外的空格)...
// Create a workable array
$i=0; // Start our array key counter
foreach($item_arr as $item) {
$clean_arr[$i++] = trim($item);
}
// See what we have
echo '
<pre>Access after cleaning:</pre>'.
'<pre>'.$clean_arr[0].'x <--no space</pre>'.
'<pre>'.$clean_arr[1].' <--no space</pre>';
?>
输出:
Common Waxbill
Estrilda astrild
Access array directly:
Common Waxbill x <--notice the trailing space
Estrilda astrild <--notice the preceding space
Access after cleaning:
Common Waxbillx <--no space
Estrilda astrild <--no space
方法二:substr(strrpos())
& substr(strpos())
如果您的列表只有两项,这很有用:
<?php
// Generate our list
$list = "Common Waxbill - Estrilda astrild";
// Start splitting
$first_item = trim(substr($list, strrpos($list, '-') + 1));
$second_item = trim(substr($list, 0, strpos($list, '-')));
// See what we have
echo "<pre>substr():</pre>
<pre>$first_item</pre>
<pre>$second_item</pre>
";
?>
输出:
substr():
Estrilda astrild
Common Waxbill
注意 strrpos() and strpos() 不同,每个都有不同的语法。
如果您不使用 PHP,但想在不依赖 RegEx 的情况下使用其他语言完成这项工作,了解该语言会有所帮助。
一般来说,编程语言都带有开箱即用的工具,这也是人们选择他们所使用的语言的部分原因。
我有这个字符串:
"Common Waxbill - Estrilda astrild"
如何为连字符前后的单词编写 2 个单独的正则表达式?我想要的输出是:
"Common Waxbill"
和
"Estrilda astrild"
这很简单:
.*(?= - ) # matches everything before " - "
(?<= - ).* # matches everything after " - "
请参阅 lookaround assertions 上的教程。
如果您不能使用回顾,但您的字符串始终采用相同的格式并且不能包含多个连字符,您可以使用
^[^-]*[^ -]
表示第一个,\w[^-]*$
表示第二个(或者 [^ -][^-]*$
如果连字符后的第一个非 space 不一定是单词-字符.
一点解释:
^[^-]*[^ -]
匹配字符串的开头(锚点 ^
),后跟任意数量的不是连字符的字符,最后是不是连字符或 space 的字符(只是为了排除最后 space 来自比赛)。
[^ -][^-]*$
采用相同的方法,但反过来,首先匹配既不是 space 也不是连字符的字符,然后是任意数量的字符,不是连字符,最后是结尾字符串(锚 $
)。 \w[^-]*$
基本相同,它使用更严格的 \w
而不是 [^ -]
。这再次用于从匹配中排除连字符后的 whitespace。
另一个解决方案是在连字符上拆分字符串并删除白色 space。
两种替代方法
你的问题的主要挑战是你想要两个单独的项目。这意味着您的流程依赖于另一种语言。 RegEx 本身不解析或分隔字符串;它仅解释了我们正在寻找的内容。 您使用的语言将进行实际分离。我的答案在 PHP 中得到了你的结果,但其他语言应该有类似的解决方案。
如果您只想完成问题中的工作,并且如果您正在使用 PHP...
方法一:explode("-", $list);
-> $array[]
如果您的列表超过两项,这将很有用:
<?php
// Generate our list
$list = "Common Waxbill - Estrilda astrild";
$item_arr = explode("-", $list);
// Iterate each
foreach($item_arr as $item) {
echo $item.'<br>';
}
// See what we have
echo '
<pre>Access array directly:</pre>'.
'<pre>'.$item_arr[0].'x <--notice the trailing space</pre>'.
'<pre>'.$item_arr[1].' <--notice the preceding space</pre>';
...您可以清理每个项目并使用 trim()
将它们重新分配给新数组。这将得到您的问题要求的文本(前后没有额外的空格)...
// Create a workable array
$i=0; // Start our array key counter
foreach($item_arr as $item) {
$clean_arr[$i++] = trim($item);
}
// See what we have
echo '
<pre>Access after cleaning:</pre>'.
'<pre>'.$clean_arr[0].'x <--no space</pre>'.
'<pre>'.$clean_arr[1].' <--no space</pre>';
?>
输出:
Common Waxbill
Estrilda astrild
Access array directly:
Common Waxbill x <--notice the trailing space
Estrilda astrild <--notice the preceding space
Access after cleaning:
Common Waxbillx <--no space
Estrilda astrild <--no space
方法二:substr(strrpos())
& substr(strpos())
如果您的列表只有两项,这很有用:
<?php
// Generate our list
$list = "Common Waxbill - Estrilda astrild";
// Start splitting
$first_item = trim(substr($list, strrpos($list, '-') + 1));
$second_item = trim(substr($list, 0, strpos($list, '-')));
// See what we have
echo "<pre>substr():</pre>
<pre>$first_item</pre>
<pre>$second_item</pre>
";
?>
输出:
substr():
Estrilda astrild
Common Waxbill
注意 strrpos() and strpos() 不同,每个都有不同的语法。
如果您不使用 PHP,但想在不依赖 RegEx 的情况下使用其他语言完成这项工作,了解该语言会有所帮助。
一般来说,编程语言都带有开箱即用的工具,这也是人们选择他们所使用的语言的部分原因。