python 正则表达式 字符串包含@-_.和半角英文数字
时间: 2023-07-29 15:10:26 浏览: 53
可以使用如下正则表达式来匹配包含@-_.和半角英文数字的字符串:
```
import re
pattern = r'^[a-zA-Z0-9@\-_.]+$'
string = 'example@1234-abcd_.'
match = re.match(pattern, string)
if match:
print('匹配成功')
else:
print('匹配失败')
```
这里的正则表达式 `^[a-zA-Z0-9@\-_.]+$` 的含义是:
- `^` 表示匹配字符串的开头
- `[a-zA-Z0-9@\-_.]+` 表示匹配至少一个字符,字符可以是a-z、A-Z、0-9、@、-、_、.
- `$` 表示匹配字符串的结尾
因此,该正则表达式可以匹配包含@-_.和半角英文数字的任何字符串。
相关问题
使用正则表达式限制QLineEdit输入范围-999.99~999.99
可以使用以下正则表达式来限制QLineEdit输入范围为-999.99~999.99:
```
^(-?\d{1,3}(.\d{1,2})?|999.99)$
```
解释:
- `^` 匹配字符串开头。
- `(-?\d{1,3}(.\d{1,2})?|999.99)` 匹配范围为-999.99~999.99的数字。
- `-?` 表示负号可选。
- `\d{1,3}` 匹配1~3位整数。
- `(.\d{1,2})?` 匹配小数部分,可选,且小数点后最多2位。
- `|` 表示或。
- `999.99` 匹配999.99。
- `$` 匹配字符串结尾。
将该正则表达式设置为QLineEdit的输入限制即可:
```python
import re
from PyQt5.QtWidgets import QLineEdit, QApplication
class MyLineEdit(QLineEdit):
def __init__(self, parent=None):
super(MyLineEdit, self).__init__(parent)
regx = re.compile('^(-?\d{1,3}(.\d{1,2})?|999.99)$')
validator = QRegExpValidator(regx, self)
self.setValidator(validator)
if __name__ == '__main__':
app = QApplication([])
line_edit = MyLineEdit()
line_edit.show()
app.exec_()
```
Python正则表达式去除邮箱@qq.com
以下是Python正则表达式去除邮箱@qq.com的例子:
```python
import re
# 定义待处理的字符串
str = "contact@qq.com 1973536419@qq.com feedback@yiibai.com"
# 定义正则表达式
pattern = r'\b\w+@[a-z0-9]+\.[a-z]+\.?[a-z]+\b'
# 使用sub()函数将匹配到的邮箱替换为空字符串
result = re.sub(pattern, '', str)
# 输出结果
print(result) # 输出: 。
```