PHP如何preg_match大于/小于文本中间的符号?

时间:2017-01-05 20:09:57

标签: php regex preg-match

我已经搜索了一百万个类似的问题,并且找不到这个特定情景的答案。

问题:

<?php
$text  = "some words    |  other words";
$text2 = "some words    >  other words";
$text3 = "some words    <  other words";

print preg_match("/\s+\|\t/", $text);  //this works, will print 'true'
print preg_match("/\s+(<|>|\|)\t/", $text2); //never true for $text2 or $text3
print preg_match("/\s+[<\|>]\t/", $text3);  //never true for $text2 or $text3
?>

我需要一些适用于所有3种情况的东西。

我尝试过后视并向前看,我无法弄清楚那些有效的东西。那些对我来说仍然有些陌生。

2 个答案:

答案 0 :(得分:0)

我不确定我是否理解你的问题。但是,如果您想匹配|<>,那么您可以使用这样的简单正则表达式:

[|<>]

如果你想匹配空格和单词,那么你可以使用这样的东西:

\w+\s+[|<>]\s+\w+

<强> Working demo

enter image description here

答案 1 :(得分:0)

你们是对的。我的代码已经有效了。我遇到的问题是当有'&lt;'时而不是'|'代码不再使用它后面的选项卡,它只在那里做空格....这是出乎意料的,很难通过看它来区分它。

感谢所有帮助。它似乎比它复杂得多。在排除故障时很难区分空格和制表符。是的,那些空格/标签必须在那里。