正则排除[^ \n\v\t\:\(\)\%\.\/\-\<\>\_\=]
时间: 2023-09-05 21:14:10 浏览: 30
你想要一个正则表达式来排除除了空格、换行符、制表符、冒号、括号、百分号、点、斜杠、连字符、小于号、大于号、下划线和等号之外的字符吗?如果是这样,可以使用以下正则表达式:
```regex
[^ \n\v\t\:\(\)\%\.\/\-\<\>\_\=]
```
这个正则表达式使用了否定字符类(^)来匹配除了在方括号中列举出的字符之外的任意字符。
相关问题
前端 <v:imagedata src="file:///C:/Users/PC/AppData/Local/Temp/msohtmlclip1/01/clip_image001.png" o:title=""/> </v:shape><![endif]--><!-- [if !vml]--><img src="file:///C:/Users/PC/AppData/Local/Temp/msohtmlclip1/01/clip_image002.jpg" width="623" height="443" /><!--[endif]--></span></p>截取所有src="file:///C:/Users/PC/AppData/Local/Temp/msohtmlclip1/的字符串
可以使用正则表达式来匹配符合要求的子字符串,然后使用字符串的 `match()` 方法或者正则表达式的 `exec()` 方法来获取匹配的结果。
例如,以下是使用 `match()` 方法实现的代码:
```javascript
const str = '<v:imagedata src="file:///C:/Users/PC/AppData/Local/Temp/msohtmlclip1/01/clip_image001.png" o:title=""/> </v:shape><![endif]--><!-- [if !vml]--><img src="file:///C:/Users/PC/AppData/Local/Temp/msohtmlclip1/01/clip_image002.jpg" width="623" height="443" /><!--[endif]--></span></p>';
const regex = /src="file:\/\/\/C:\/Users\/PC\/AppData\/Local\/Temp\/msohtmlclip1\//g;
const matches = str.match(regex);
console.log(matches); // ['src="file:///C:/Users/PC/AppData/Local/Temp/msohtmlclip1/', 'src="file:///C:/Users/PC/AppData/Local/Temp/msohtmlclip1/']
```
上述正则表达式使用了转义字符 `\` 来匹配 `file:///C:/Users/PC/AppData/Local/Temp/msohtmlclip1/` 字符串,使用 `g` 参数来匹配所有符合要求的子字符串。
注意,此处的 `match()` 方法返回的是一个数组,包含了所有匹配的结果。如果没有匹配到任何结果,返回的是 `null`。
使用正则表达式提取<tag>..... </tag>范国内的数字 e.g.s:一个字串" <htmL>this is head <tog>this is a Lovely day for 2230238212, end. </tag> </html> ”输出: 2230238212 :param s: :return: int
可以使用Python中re模块中的正则表达式来提取数字。具体实现如下:
```python
import re
def extract_number(s):
pattern = r'<tag>.*?(\d+).*?</tag>'
match = re.search(pattern, s)
if match:
return int(match.group(1))
else:
return None
```
解释:
- `r'<tag>.*?(\d+).*?</tag>'` 是用来匹配包含数字的 `<tag>` 和 `</tag>` 标签的正则表达式。
- `.*?` 表示非贪婪匹配,即匹配最小长度的字符串。
- `(\d+)` 表示匹配数字,并且使用括号将数字部分分组。
- `match.group(1)` 返回第一个括号中匹配到的内容,即数字部分。
- 如果没有找到数字,则返回 `None`。
使用示例:
```python
s = "<htmL>this is head <tog>this is a Lovely day for 2230238212, end. </tag> </html>"
result = extract_number(s)
print(result) # 输出 2230238212
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
用正则表达式.?board-index.?>(.?).?data-src="(.?)".?name.?a.?>(.?)
.?star.?>(.?).?releasetime.?>(.?).?integer.?>(.?).?fraction.?>(.?).?爬取猫眼电影TOP100的所有信息。网址:https://www.maoyan.com/board/4 ![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
用正则表达式.*?board-index.*?>(.*?).*?data-src="(.*?)".*?name.*?a.*?>(.*?)
.*?star.*?>(.*?).*?releasetime.*?>(.*?).*?integer.*?>(.*?).*?fraction.*?>(.*?).*?爬取猫眼电影排行:爬取猫眼电影TOP100的所有信息。网址:https://www.maoyan.com/board/4 ![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)