python 正则表达式 前瞻_我应该如何在python中编写这个正则表达式(How should I write this regex in python)...
我应该如何在python中编写这个正则表达式(How should I write this regex in python)
我有字符串。
st = "12345 hai how r u @3456? Awer12345 7890"
re.findall('([0-9]+)',st)
它应该不会像:
['12345', '3456', '12345', '7890']
我应该得到
['12345','7890']
我应该只取数值
和
它不应该包含任何其他字符,如字母,特殊字符
I have the string.
st = "12345 hai how r u @3456? Awer12345 7890"
re.findall('([0-9]+)',st)
It should not come like :
['12345', '3456', '12345', '7890']
I should get
['12345','7890']
I should only take the numeric values
and
It should not contain any other chars like alphabets,special chars
原文:https://stackoverflow.com/questions/9216382
更新时间:2019-12-16 16:50
最满意答案
In [21]: re.findall(r'(?:^|\s)(\d+)(?=$|\s)', st)
Out[21]: ['12345', '7890']
这里,
(?:^|\s)是一个非捕获组,它匹配字符串的开头或空格。
(\d+)是一个匹配一个或多个数字的捕获组。
(?=$|\s)是前瞻性断言,它匹配字符串的末尾或空格, 而不消耗它 。
In [21]: re.findall(r'(?:^|\s)(\d+)(?=$|\s)', st)
Out[21]: ['12345', '7890']
Here,
(?:^|\s) is a non-capture group that matches the start of the string, or a space.
(\d+) is a capture group that matches one or more digits.
(?=$|\s) is lookahead assertion that matches the end of the string, or a space, without consuming it.
2012-02-09
相关问答
In [21]: re.findall(r'(?:^|\s)(\d+)(?=$|\s)', st)
Out[21]: ['12345', '7890']
这里, (?:^|\s)是一个非捕获组,它匹配字符串的开头或空格。 (\d+)是一个匹配一个或多个数字的捕获组。 (?=$|\s)是前瞻性断言,它匹配字符串的末尾或空格, 而不消耗它 。 In [21]: re.findall(r'(?:^|\s)(\d+)(?=$|\s)', st)
Out[21]: ['12345', '7890']
H
...
>>> import re
>>> regex = re.compile('(?:red,(?P\w+)|(?P\w+),red)')
>>> string = "blue,red red,yellow blue,yellow red,green purple red, ..."
>>> for matches in regex.finditer(string):
... if matches.group('redf
...
如果使用Python 2.7,请使用Unicode字符串。 我假设你的“我需要的”例子是不正确的,或者你真的想要AAAAA用于طس ? 如果从文件中读取字符串,请首先将字符串解码为Unicode。 #!python2
#coding: utf8
import re
# Note leading u
data = u'TX 35-L|М-21|A 1 طس|US-50|yeni sinop-erfelek yolu çevre yolu|Av Antônio Ribeiro'.split('|')
...
>>> import re
>>> strs = 'The output is\n1) python\nA dynamic language\neasy to learn\n2) C++\ndifficult to learn\n3244) PHP\neay to learn\n'
>>> re.findall(r'\d+\)\s[^\d]+',strs)
['1) python\nA dynamic language\neasy to learn\n',
'2) C++\ndifficult t
...
(从技术上讲,多行字符串!=多行注释。但这不是重点) 正则表达式(['"])\1\1(.*?)\1{3}应该有效,但请确保使用re.DOTALL 。 (['"])找到'或"并在\1捕获它 \1\1找到2个相同的引号 (.*?)抓住一切,直到... \1{3}找到三个相同的引号 (Technically, multiline strings != multiline comments. But that's aside from the point) The regex (['"])\1\1(.*?
...
您可以使用 date ([a-z]{3} \d{2}) at (\d{2}) ([PA]M)
见演示 比较你的两个选择: date ([a-z]{3} [0-9]{2}) at ([0-9]{2}) ([P][M])
date ([a-z]{3} [0-9]{2}) at ([0-9]{2}) ([A][M])
注意它们有多相似。 我们只需为PM或AM添加1个替代方案。 可以使用与P或A匹配的字符类[PA]来完成。 而不是[0-9] ,你可以使用速记类\d (它有点短:),并且不要忘记将正则表
...
使用正则表达式非捕获组和regex.findAll函数的解决方案: import regex
...
fh = open('lines.txt', 'r'); // considering 'lines.txt' is your initial file
commlines = fh.read()
_sched_wakeup_pattern = regex.compile(r"""
comm=(?P[\S]+?)
\spid=(?P\d+)
\spri
...
[^\W\d]
丢弃非单词字符并丢弃数字。 保持休息。 [^\W\d]
Throw out non-word characters and throw out digits. Keep the rest.
阅读文档 。 re.findall返回组,如果有的话。 如果您想要整个匹配,则必须将其全部分组,或使用re.finditer 。 看到这个问题 。 Read the documentation. re.findall returns the groups, if there are any. If you want the entire match you must group it all, or use re.finditer. See this question.
尝试使用 \[xxxcixxx\[\[_'.*?'\] \[_'.*?'\]\]xxxcixxx\]
演示: http : //regexr.com/3d887 Try using \[xxxcixxx\[\[_'.*?'\] \[_'.*?'\]\]xxxcixxx\]
Demo: http://regexr.com/3d887
python 正则表达式 前瞻_我应该如何在python中编写这个正则表达式(How should I write this regex in python)...相关推荐
- 如何在Ruby中编写switch语句
如何在Ruby中编写switch语句? #1楼 案例...当 在Chuck的答案中添加更多示例: 带参数: case a when 1puts "Single value" whe ...
- 如何在Go中编写多行字符串?
本文翻译自:How do you write multiline strings in Go? Does Go have anything similar to Python's multiline ...
- 如何在 Go 中编写 Switch 语句
如何在 Go 中编写 Switch 语句 目录 在 Go 中导入包 理解 Go 中包的可见性 如何在 Go 中编写条件语句 如何在 Go 中编写 Switch 语句 如何在 Go 中构造 for 循环 ...
- 「每周译Go」如何在 Go 中编写 Switch 语句
目录 在 Go 中导入包 理解 Go 中包的可见性 如何在 Go 中编写条件语句 如何在 Go 中编写 Switch 语句 如何在 Go 中构造 for 循环 在循环中使用 Break 和 Conti ...
- linux命令行运行c程序,如何在Linux中编写和运行C程序
Linux正在成为开发人员的编程天堂,成为开源和免费操作系统. Turbo C编译器已经是一种编译程序的旧方法,所以让程序员转向Linux以获得新的编程环境. 在本文中,我们将解释如何编写,编译和运行 ...
- 如何在Go中编写防弹代码:不会失败的服务器工作流程
by Tal Kol 通过塔尔科尔 如何在Go中编写防弹代码:不会失败的服务器工作流程 (How to write bulletproof code in Go: a workflow for ser ...
- eclipse 导入项目_JAVA编程实战:坦克大战系列2-坦克如何在eclipse中编写
游戏中寻找学习JAVA的乐趣之 坦克大战系列2-坦克如何在Eclipse中编写 前言 本篇主要对Robocode在eclipse中如何配置并编写. Eclipse中的配置 通过本身自带的编辑器去写代码 ...
- matlab矩阵指定行最大值,求Matlab程序:在2行矩阵中,如何求第1行最大值和第2行相应的最大,请问,如何在MATLAB中编写程序实现求两矩阵A*B,A.*...
导航:网站首页 > 求Matlab程序:在2行矩阵中,如何求第1行最大值和第2行相应的最大,请问,如何在MATLAB中编写程序实现求两矩阵A*B,A.* 求Matlab程序:在2行矩阵中,如何求 ...
- 手机nfc_如何在Android中编写NFC标签
手机nfc 这篇文章介绍了如何在Android中使用NFC编写智能标签. Android智能手机不仅能够读取包含URL,电话号码等数据的NFC标签,但使用Android NFC Api可以写入NFC标 ...
最新文章
- 用D3.js 十分钟实现字符跳动效果
- 修改sms_def的MOF文件收集网络共享信息
- BZOJ2062 : 素颜2(face2)
- js中执行到一个if就停止的代码_Node.JS实战64:ES6新特性:Let和Const。
- 国产化达梦数据库数据迁移文档:oracle11g数据库转达梦8数据库实例演示
- ACM竞赛学习整理开篇之01背包问题
- python3精要(5)-最长公共前缀Trie树
- 时间序列与R语言应用(part1)--时间序列基本概念
- [MyBatisPlus]通用Service接口测试通用Service
- Mybatis源码之插件模块分析
- 【Python】Python一些有趣而基础的知识(结合Java进行对比)
- 分享Silverlight/WPF/Windows Phone一周学习导读(12月13日-12月19日)
- linux下设置opencv环境变量
- bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐(暴力DFS)
- NSRunLoop中Autorelease pool 管理
- python7.2抛出自定义异常
- 巴斯克维尔字体标本案例研究
- 解决invalid operands of types ‘float‘ and ‘int‘ to binary ‘operator %
- 彻底弄懂 Nginx location 匹配
- Python爬虫实战一之使用Beautiful Soup抓取百度招聘信息并存储excel文件
热门文章
- 20121022 django学习笔记1
- 在类内定义成员函数、在类外定义成员函数、计算长方体的体积【C++面向对象编程类的使用经典案例】
- html之CSS设计(float定位和position定位详细分析)
- 初学者python笔记(re模块、正则表达式完全解析)
- html语言中base,HTML base 标签
- 数组后存入数据、删除指定内容数据
- Web加固linux,Linux系统下web服务器的加固
- Python+sklearn训练结果保存与加载(以垃圾邮件分类为例)
- 微课|中学生可以这样学Python(例11.2):tkinter猜数游戏(1)
- foxmail 服务器备份 立刻删除_Foxmail删除服务器邮件而保留本地备份的详细操作方法...