简体   繁体   English

正则表达式在字符串中查找某些URL

[英]Regex to Find Certain URLs in String

I'm not very good at Regex, but I've been working on improving. 我不是很擅长正则表达,但我一直在努力改进。 Right now I'm trying to make a regex expression to match all URLs in a string that follows a certain syntax. 现在我正在尝试使用正则表达式来匹配遵循特定语法的字符串中的所有URL。 Here is my regex code: 这是我的正则表达式代码:

@http://api\.ning\.com:80/files/[a-z0-9\*]/[a-z0-9\.]\.[jpg|png|gif|bmp]@i

Here is an example of something I want this to match (but it isn't matching): 这是我想要匹配的一个例子(但它不匹配):

http://api.ning.com:80/files/etWx3bZZxVPTI8A3sSd3zoLhhkTmjoCs2IRFnOacPoHzJogudMCze2mB2Fib0Z*R/ScreenShot20131111at4.58.13PM.png?width=375

You need to do two things: 你需要做两件事:

  1. Add a + at the end of group of characters [a-z0-9*] and [a-z0-9.] 在字符组[a-z0-9 *]和[a-z0-9。]的末尾添加一个+
  2. Move the file extensions in parentheses instead of square brackets 将文件扩展名移到括号中而不是方括号中

The expression would become: 表达式将成为:

@http://api\.ning\.com:80/files/[a-z0-9\*]+/[a-z0-9\.]+\.(jpg|png|gif|bmp)@i

which can be further simplified to: 可以进一步简化为:

@http://api\.ning\.com:80/files/[\w\*]+/[\w\.]+\.(jpg|png|gif|bmp)@

Note that \\w already includes upper-case and lower-case letters as well as numbers. 请注意\\ w已包含大写和小写字母以及数字。 So, you can get rid of the i flag too unless you need it for the file extensions or the base URL. 因此,除非您需要文件扩展名或基本URL,否则您也可以删除i标志。

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

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