简体   繁体   English

与以下模式匹配的正则表达式

[英]regular expression that matches the below pattern

I'm not good in regular expression, but today I faced an unavoidable situation, So I need a regular expression that matches the case below: 我的正则表达式不好,但是今天我遇到了不可避免的情况,因此我需要一个匹配以下情况的正则表达式:

|hhas.jpg||sd22-9393das.png||8jjas.png||IMG00338-20110109.jpg|

I tried this regex : /(?<=\\|)(\\w|\\d+\\.\\w+)(?=\\|)/i but not getting the desired results... 我尝试了此正则表达式:/(?<= /(?<=\\|)(\\w|\\d+\\.\\w+)(?=\\|)/i但未获得所需的结果...

I want to match all the strings by using preg_match function of PHP between two | 我想通过在两个|之间使用PHP的preg_match函数来匹配所有字符串| signs ie hhas.jpg, sd22-9393das.png etc... 标志,例如hhas.jpg,sd22-9393das.png等...

Use this.. 用这个..

preg_match_all('/\|(.*?)\||/', $str, $matches);
print_r(array_filter($matches[1]));

OUTPUT :

Array
(
    [0] => hhas.jpg
    [1] => sd22-9393das.png
    [2] => 8jjas.png
    [3] => IMG00338-20110109.jpg
)

Demonstration 示范

enter image description here

You can use the following regex: 您可以使用以下正则表达式:

/\\|([^|]*)\\|/gi

Demo 演示

Matched strings: 匹配的字符串:

1. hhas.jpg
2. sd22-9393das.png
3. 8jjas.png
4. IMG00338-20110109.jpg

your expression : 你的表情:

/(?<=\|)(\w|\d+\.\w+)(?=\|)/i

pretty well written , but just has a few minor flaws 写得不错,但有一些小缺陷

  • when you say \\w that is only one character. 当您说\\w ,这只是一个字符。

  • the OR condition OR条件

  • \\d+\\.\\w+ will match only when it meets the same order. \\d+\\.\\w+仅在满足相同顺序时匹配。 ie list of digits first followed by a . 即数字列表的前面跟随一个. and then followed by letters or digits or underscore. 然后是字母或数字或下划线。

better change your regex to : 最好将您的正则表达式更改为:

/(?<=\|)(.*?)(?=\|)/ig

this will give you anything which is between | 这会给你介于两者之间的任何东西| s 小号

also IMHO , using lookarounds for such a problem is an overkill. 同样是恕我直言,使用环视解决此类问题实在是太过分了。 Better use : 更好地使用:

/\|(.*?)\|/ig

Try without using regular expression. 尝试不使用正则表达式。

explode('||', rtrim(ltrim ('|hhas.jpg||sd22-9393das.png||8jjas.png||IMG00338-20110109.jpg|','|'),'|'));

Output: 输出:

Array ( [0] => hhas.jpg [1] => sd22-9393das.png [2] => 8jjas.png [3] => IMG00338-20110109.jpg ) 

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

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