python中的正则表达式捕获第一个运算符(Regular expression in python to capture first operator)
我试图解析以下模式:
name1运算符name2
其中'operator'是&,^或|之一
这些组应该是
(name1 operator name2,name1,operator,name2)
现在,任一名称都可以包含
&,| or ^
&,| or ^
和空格,但只有第一次出现才会使操作员。 进一步的出现将被视为name2的一部分。我已经有很长一段时间没跟这个了,我有
\s*(\w+\s*\w*)\s*([&|^])\s*(\w+\s*\&*\w+)
看起来不是很脆弱,也不起作用。 此外,如果有一个类似于
str.partition()
的函数可以使用正则表达式并将结果限制为1,那就太棒了。 我找不到一个。正则表达式或函数的任何想法? 谢谢
I am trying to parse the following pattern:
name1 operator name2
where 'operator' is one of &,^ or |
The groups should be
(name1 operator name2,name1,operator,name2)
Now, either name can also include
&,| or ^
and white spaces, but only the first appearance will make the operator. Further appearances will be considered to be a part of name2.I've been sturggling with this for quite some time now, and I have
\s*(\w+\s*\w*)\s*([&|^])\s*(\w+\s*\&*\w+)
Doesn't seem very cleaver, doesn't work either. Also, if there's a function similar to
str.partition()
that can work with regex and limit the results to 1, it would be great. I couldn't find one.Any ideas for a regex or function? Thanks
原文:https://stackoverflow.com/questions/15483115
最满意答案
虽然
basename
可以在文件扩展名上使用,但使用shell参数扩展功能更容易:for file in *.foo; do mv "$file" "${file%.foo}.bar"; done
你的
basename
代码不起作用,因为basename
只运行一次,然后xargs每次只看{}.bar
。Although
basename
can work on file extensions, using the shell parameter expansion features is easier:for file in *.foo; do mv "$file" "${file%.foo}.bar"; done
Your code with
basename
doesn't work because thebasename
is only run once, and then xargs just sees{}.bar
each time.
相关问答
更多-
使用其他可用工具在两个命令之间过滤流。 对于您的具体示例,请使用tail : cmd1 | tail -n +2 | cmd2 您可能会发现其他有价值的命令是head , grep和sed 。 Use other available tools to filter the stream in between your two commands. For your specific example, use tail: cmd1 | tail -n +2 | cmd2 Other commands you ...
-
在我的邮件版本(Debian linux 4.0)中,以下选项用于控制源/回复地址 -a开关,要应用的附加头,在命令行上提供将附加到传出邮件头的From:头 $ REPLYTO环境变量指定一个Reply-To:标题 所以顺序如下 export REPLYTO=cms-replies@example.com mail -aFrom:cms-sends@example.com -s 'Testing' 在我的邮件客户端中,结果是来自cms-sends@example.com的邮件,其中任何回复将默认为cms- ...
-
\R转义序列 Perl v5.10 + ; 请参阅perldoc rebackslash或在线文档 ,该文档与“通用换行符”(平台无关) 可以在此处合作(示例使用Bash创建多行输入字符串): $ printf 'foo barmy\r\nfoo baryour\r\n' | perl -pe 's/foo bar(.*?)\R/foo $1 bar\n/gm' foo my bar foo your bar 请注意,与以太的答案唯一的区别是使用非贪婪构造 ( .*?而不是仅仅.* ),这在这里有所不同。 ...
-
单行解决方案,将一个文件中的行匹配到另一个文件(One liner solution to matching lines in one file to another file)[2021-12-09]
请使用grep : # long version grep --fixed-strings --word-regexp --file=foo bar # short version grep -F -w -f foo bar # mini version grep -Fwffoo bar 结果 beer 25 bat 78 Please use grep: # long version grep --fixed-strings --word-regexp --file=foo bar # short v ... -
fmt | sed '/^$/d' fmt命令将包装75个字符的行,所以使用fmt -w [WIDTH]设置更长的行。 fmt | sed '/^$/d' The fmt command will wrap lines at 75 characters, so use fmt -w [WIDTH] to set longer lines.
-
使用SED或PERL单行匹配并替换多条换行符(match and replace multiple newlines with a SED or PERL one-liner)[2022-04-03]
对于“perl -pe”,你的问题是它正在逐行处理,所以没有办法找到“\ n \ n”。 如果您将-0777标志添加到Perl中(以使其一次处理整个文件),它将起作用: perl -0777 -pe "s|(\n\n//supercrazytag)|\nvoid func_new();$1|g" myfile.c 我还将(此用法不推荐使用)\ 1更改为$ 1,并在替换开始处添加了一个额外的“\ n”以提高可读性。 请参阅perlrun(命令开关) ,了解奇怪的“-0777” For "perl -pe", ... -
我认为这与支撑间距有关,而不是与return ,所以我会检查支撑的空间设置。 我试图使用的第一个选项是sp_inside_braces (默认情况下应该是“忽略”)。 I think that this has more to do with brace spacing than with the return, so I'd check the space settings for braces. The first option I would try to play with would be sp_ ...
-
虽然basename可以在文件扩展名上使用,但使用shell参数扩展功能更容易: for file in *.foo; do mv "$file" "${file%.foo}.bar"; done 你的basename代码不起作用,因为basename只运行一次,然后xargs每次只看{}.bar 。 Although basename can work on file extensions, using the shell parameter expansion features is easier: ...
-
我相信它们之间没有区别,但根据我的经验,最常用的形式是Foo::bar() 。 你可以在这里找到一些例子。 有静态方法示例和用法:
shell one-liner仅在其ASCII格式文件时才捕获文件(shell one-liner to cat a file only if its ASCII format file)[2022-07-16]
您可以使用带有--mime和-b选项的file命令的输出。 $ file -b --mime filename.bin application/octet-stream; charset=binary -b选项禁止在输出中打印文件名,因此您不必担心错误匹配文件名,而且--mime会为您提供字符集。 您可以使用grep来测试charset=binary的出现 $ file -b --mime filename.bin | grep -q "charset=binary" 然后,您可以使用grep的退出状 ...相关文章
更多- 快速了解正则表达式
- Python学习笔记
- Python基础 运算
- 揭开正则表达式的神秘面纱
- 正则表达式 - 语法
- Python内建函数(E)
- 探索 Python,第 1 部分: Python 的内置数值类型
- python2和python3的区别
- Python的文件类型
- python的下载与安装
最新问答
更多- 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
- 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
- OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
- 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
- codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
- 在计算机拍照在哪里进入
- 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
- No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
- 单页应用程序:页面重新加载(Single Page Application: page reload)
- 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
- System.StackOverflow错误(System.StackOverflow error)
- KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
- 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
- android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
- TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
- 企业安全培训的各项内容
- 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
- C#类名中允许哪些字符?(What characters are allowed in C# class name?)
- NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
- 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
- 将多个行和可变行移动到列(moving multiple and variable rows to columns)
- 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
- 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
- Angular $资源不会改变方法(Angular $resource doesn't change method)
- 在Angular 5中不是一个函数(is not a function in Angular 5)
- 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
- 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
- 常见的python rpc和cli接口(Common python rpc and cli interface)
- Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
- 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)