Reg Ex帮助 [英] Reg Ex help

查看:102
本文介绍了Reg Ex帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个clearcase cleartool ls命令的字符串。


/main/parallel_branch_1/release_branch_1.0/dbg_for_python/CHECKEDOUT
来自/ main /的
parallel_branch_1 / release_branch_1.0 / 4

我想写一个正则表达式给我分支文件是

结帐,在这种情况下 - '' dbg_for_python''


如果有比使用正则表达式更好的方法,请告诉我。


提前致谢,

Don

I have a string from a clearcase cleartool ls command.

/main/parallel_branch_1/release_branch_1.0/dbg_for_python/CHECKEDOUT
from /main/parallel_branch_1/release_branch_1.0/4

I want to write a regex that gives me the branch the file was
checkedout on ,in this case - ''dbg_for_python''

Also if there is a better way than using regex, please let me know.

Thanks in advance,
Don

推荐答案

don写道:
我有一个清晰的cleartool字符串ls命令。

/main/parallel_branch_1/release_branch_1.0/dbg_for_python/CHECKEDOUT
来自/main/parallel_branch_1/release_branch_1.0/4

我想写一个正则表达式给我分支文件是
结帐,在这种情况下 - ''dbg_for_python''

如果有比使用正则表达式更好的方法,请让我知道。

提前致谢,
Don
I have a string from a clearcase cleartool ls command.

/main/parallel_branch_1/release_branch_1.0/dbg_for_python/CHECKEDOUT
from /main/parallel_branch_1/release_branch_1.0/4

I want to write a regex that gives me the branch the file was
checkedout on ,in this case - ''dbg_for_python''

Also if there is a better way than using regex, please let me know.

Thanks in advance,
Don




不是正则表达式,但是这样做你想要的吗?



Not regex, but does this do what you want?

s =" /main/parallel_branch_1/release_branch_1.0/dbg_for_python/CHECKEDOUT"
s = s +"来自/main/parallel_branch_1/release_branch_1.0/4"
s.split(''/'')[4]
s = "/main/parallel_branch_1/release_branch_1.0/dbg_for_python/CHECKEDOUT"
s = s + " from /main/parallel_branch_1/release_branch_1.0/4"
s.split(''/'')[4]



' 'dbg_for_python''


''dbg_for_python''


> /main/parallel_branch_1/release_branch_1.0/dbg_for_python/CHECKEDOUT
> /main/parallel_branch_1/release_branch_1.0/dbg_for_python/CHECKEDOUT
来自/main/parallel_branch_1/release_branch_1.0/4

我想写一个给我分支的正则表达式这个文件是
结帐,在这种情况下 - ''dbg_for_python''

如果有比使用正则表达式更好的方法,请告诉我。
from /main/parallel_branch_1/release_branch_1.0/4

I want to write a regex that gives me the branch the file was
checkedout on ,in this case - ''dbg_for_python''

Also if there is a better way than using regex, please let me know.



好​​吧,如果你把它全部放在一个字符串中:


s =

" / main / parallel_branch_1 / release_branch_1 .0 / dbg_for_python / CHECKEDOUT

来自/main/parallel_branch_1/release_branch_1.0/4"


你可以做到


branch = s.split(" /")[4]


返回分支,假设来自root的路径是

对于每个相关项目都一样。


如果没有,你可以修补像


r = re.compile(r''/( [^ /] *)/ CHECKEDOUT'')

m = r.match(s)


和w hich应该使m.groups(1)得到项目。你好吗<
没有详细说明什么是常数(路径中的子目录数量是多少?CHECKEDOUT

部分?等等,所以很难弄清楚全球适用的最多的是什么。


-tkc



Well, if you have it all in a single string:

s =
"/main/parallel_branch_1/release_branch_1.0/dbg_for_python/CHECKEDOUT
from /main/parallel_branch_1/release_branch_1.0/4"

you can do

branch = s.split("/")[4]

which returns the branch, assuming the path from root is the
same for each item in question.

If not, you can tinker with something like

r = re.compile(r''/([^/]*)/CHECKEDOUT'')
m = r.match(s)

and which should make m.groups(1) the resulting item. You
don''t give much detail regarding what is constant (the
number of subdirectories in the path? the CHECKEDOUT
portion?, etc) so it''s kinda hard to figure out what is most
globally applicable.

-tkc


你好,


可能有更好的方法然后正则表达式,虽然我觉得它们很有用

并使用它们很多。


他们的工作方式取决于对某些事情的了解。对于

的例子,如果您所追求的目标总是4

在结构深处,您可以尝试类似......


path =

''/ main/parallel_branch_1/release_branch_1.0/dbg_for_python/CHECKEDOUT

来自/main/parallel_branch_1/release_branch_1.0/4的
' '


p = re.compile(''/ \ S * / \ S * / \ S * /(\ S *)/'')

m = re.search(p,path)

..

如果m:

print m.group(1)

这是一个很好的参考...
http://www.amk.ca/python/howto/regex/

希望有所帮助,

aaron。


don写道:
Hi don,

there may well be a better way then regex, although I find them usefull
and use them a lot.

The way they work would be dependant on knowing some things. For
example, if the dir you are after is always 4
deep in the structure you could try something like...

path =
''/main/parallel_branch_1/release_branch_1.0/dbg_for_python/CHECKEDOUT

from /main/parallel_branch_1/release_branch_1.0/4''

p = re.compile(''/\S*/\S*/\S*/(\S*)/'')
m = re.search(p, path)
..
if m:
print m.group(1)
This is a good reference...
http://www.amk.ca/python/howto/regex/

Hope that helps,
aaron.

don wrote:
我有一个来自clearcase cleartool ls命令的字符串。

/ main / parallel_branch_1 / release_branch_1 。/ dbg_for_python / CHECKEDOUT
来自/main/parallel_branch_1/release_branch_1.0/4

我想写一个rege x给我分支文件是
签出的,在这种情况下 - ''dbg_for_python''

如果有比使用正则表达式更好的方法,请告诉我。

提前致谢,
I have a string from a clearcase cleartool ls command.

/main/parallel_branch_1/release_branch_1.0/dbg_for_python/CHECKEDOUT
from /main/parallel_branch_1/release_branch_1.0/4

I want to write a regex that gives me the branch the file was
checkedout on ,in this case - ''dbg_for_python''

Also if there is a better way than using regex, please let me know.

Thanks in advance,
Don






这篇关于Reg Ex帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆