求个正则的查询: 查询包含指定字符串“ABCD”,且不包含字符“/”的写法 谢谢!

aiur 2009-01-22 12:50:06
求个正则的查询: 查询字符串里包含指定字符串“ABCD”,且不包含字符“/”的写法 谢谢!
...全文
334 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
hgscsjw 2009-03-26
  • 打赏
  • 举报
回复
13楼的牛人,才是真正正解,前12楼的人都白活了,我是昨天才知道正则表达式都看懂了,你们中居然还有人说不可能实现,向13楼致敬!
wxq4100798 2009-01-22
  • 打赏
  • 举报
回复
"/^([^\/]*ABCD[^\/]*)$/i"
aiur 2009-01-22
  • 打赏
  • 举报
回复
看来是php里可以,editplus里不可以
谢谢两位的解答,结贴!
程序猿之殇 2009-01-22
  • 打赏
  • 举报
回复
呵呵,我没玩过editplus,只是站在php的角度上来年问题.
所以...
dubiousway 2009-01-22
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 jakey9826 的回复:]
引用 5 楼 dubiousway 的回复:
3楼的似乎不对。
/([^\/]+ABCD[^\/]+)/ 这个模式并不能规定abcd之前或之后 不存在'/',只是规定,abcd前后,至少有1个字符不是'/'

很简单,再加个开头结尾就好了.



PHP code$s='13fgkjfweriabcdetoype';
if(!preg_match("/^([^\/]+ABCD[^\/]+)$/i", $s, $matches))
{
echo "不匹配";
}
else
{
echo "匹配";
}
[/Quote]

还是8楼说的对,
如果lz说的要匹配的字符串,是指整整一行的话,8楼的应该挺对的。
dubiousway 2009-01-22
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 aiur 的回复:]
$s1 = "ABCD /";
$s2 = "ABCD .xml";
preg_match_all("/([^\/]+ABCD[^\/]+)/i", $s1, $matches);
var_dump($matches);
preg_match_all("/([^\/]+ABCD[^\/]+)/i", $s2, $matches);
var_dump($matches);

我用这个测试,打印出的$matches 似乎是2个元素为空的数组。

4楼的同学说的,--正则似乎不能匹配“不是某个字符串”的字符串。--
其实我就是想排除一个特殊字符“/”,不是字符串,应该用^就可以的吧

再…
[/Quote]

“/”也是字符串,只不过含有一个字符罢了。
我觉得,你说的问题,不在于是否[^]的使用,而是你的匹配问题涉及到一个“与”条件的匹配,即,满足条件1,且满足条件2,这如果用一个匹配模式,通过一次匹配匹配出来,似乎不太可能(也可能我学的不精)。
就我知道,php到是支持条件匹配,格式是:(?(condition)yespattern|nopattern)

但editplus,不支持,
editplus的正则描述里,只支持选择匹配,就是'|'操作:
---------edit plus Regular Expression----------
Expression Description
\t Tab character.
\n New line.
. Matches any character.
| Either expression on its left and right side matches the target string. For example, "a|b" matches "a" and "b".
[] Any of the enclosed characters may match the target character. For example, "[ab]" matches "a" and "b". "[0-9]" matches any digit.
[^] None of the enclosed characters may match the target character. For example, "[^ab]" matches all character EXCEPT "a" and "b". "[^0-9]" matches any non-digit character.
* Character to the left of asterisk in the expression should match 0 or more times. For example "be*" matches "b", "be" and "bee".
+ Character to the left of plus sign in the expression should match 1 or more times. For example "be+" matches "be" and "bee" but not "b".
? Character to the left of question mark in the expression should match 0 or 1 time. For example "be?" matches "b" and "be" but not "bee".
^ Expression to the right of ^ matches only when it is at the beginning of line. For example "^A" matches an "A" that is only at the beginning of line.
$ Expression to the left of $ matches only when it is at the end of line. For example "e$" matches an "e" that is only at the end of line.
() Affects evaluation order of expression and also used for tagged expression.
\ Escape character. If you want to use character "\" itself, you should use "\\".

--------------------
程序猿之殇 2009-01-22
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 dubiousway 的回复:]
3楼的似乎不对。
/([^\/]+ABCD[^\/]+)/ 这个模式并不能规定abcd之前或之后 不存在'/',只是规定,abcd前后,至少有1个字符不是'/'
[/Quote]
很简单,再加个开头结尾就好了.


$s='13fgkjfweriabcdetoype';
if(!preg_match("/^([^\/]+ABCD[^\/]+)$/i", $s, $matches))
{
echo "不匹配";
}
else
{
echo "匹配";
}
dubiousway 2009-01-22
  • 打赏
  • 举报
回复
我4楼写差了:写成数字9了

$s='13fgkjfweriabcdetoyp/e';
$p1='/abcd/';
$p2='/\//';
if(!preg_match($p2,$s,$m) && preg_match($p1,$s,$m))
echo $m[0];
aiur 2009-01-22
  • 打赏
  • 举报
回复
$s1 = "ABCD /";
$s2 = "ABCD .xml";
preg_match_all("/([^\/]+ABCD[^\/]+)/i", $s1, $matches);
var_dump($matches);
preg_match_all("/([^\/]+ABCD[^\/]+)/i", $s2, $matches);
var_dump($matches);

我用这个测试,打印出的$matches 似乎是2个元素为空的数组。

4楼的同学说的,--正则似乎不能匹配“不是某个字符串”的字符串。--
其实我就是想排除一个特殊字符“/”,不是字符串,应该用^就可以的吧

再次谢谢各位啦
dubiousway 2009-01-22
  • 打赏
  • 举报
回复
3楼的似乎不对。
/([^\/]+ABCD[^\/]+)/ 这个模式并不能规定abcd之前或之后 不存在'/',只是规定,abcd前后,至少有1个字符不是'/'
dubiousway 2009-01-22
  • 打赏
  • 举报
回复
正则似乎不能匹配“不是某个字符串”的字符串。

分两步匹配的话,可以

$s='13fgkjfweriabcdetoyp/e';
$p1='/abcd/';
$p2='/9/';
if(!preg_match($p2,$s,$m) && preg_match($p1,$s,$m))
echo $m[0];


在editplus里,是否可以通过录制宏来实现两步搜索(只是个设想,呵呵)
程序猿之殇 2009-01-22
  • 打赏
  • 举报
回复
preg_match_all("/([^\/]+ABCD[^\/]+)/i", $str, $matches);
var_dump($matches);
aiur 2009-01-22
  • 打赏
  • 举报
回复
我是想在editplus的“在文件中查找”符合条件的内容。
比如我要查找“ABCD”(这个是固定的某个字符串),但是又不想查找的结果里包含“/”这个字符。
请问要如何查找?
程序猿之殇 2009-01-22
  • 打赏
  • 举报
回复
需求不明确.
你要匹配什么?
如果只是查找ABCD,根本用不到正则.

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧