ksh 风格的左右字符串剥离为匹配的表达式?

发布于 2024-09-09 11:18:01 字数 324 浏览 8 评论 0原文

如何将字符串的左侧部分和右侧部分剥离到 ksh 中的匹配表达式?

例如:(

${name##*/}

${name%/*}

参见 http://www.well. ox.ac.uk/~johnb/comp/unix/ksh.html ksh 示例)。

我似乎无法找到使用 re 模块或 string 模块来执行此操作的简单方法,但我一定错过了一些东西。

How can one strip the left parts and right parts off strings up to a matching expression as in ksh?

For instance:

${name##*/}

${name%/*}

(see http://www.well.ox.ac.uk/~johnb/comp/unix/ksh.html for ksh examples).

I can't seem to figure out a simple way of doing this using re module or string module but I must be missing something.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏末染殇 2024-09-16 11:18:01

Ksh:

$ s='abc/def/ghi'
$ echo ${s%%/*}
abc
$ echo ${s%/*}
abc/def
$ echo ${s#*/}
def/ghi
$ echo ${s##*/}
ghi

Python:

>>> s='abc/def/ghi'
>>> print s[:s.find("/")]
abc
>>> print s[:s.rfind("/")]
abc/def
>>> print s[s.find("/")+1:]
def/ghi
>>> print s[s.rfind("/")+1:]
ghi

编辑:

要处理模式丢失的情况,如ΤΖΩΤΖIΟΥ所指出的:

>>> s='abc/def/ghi'
>>> t='no slash here'
>>> print s[:s.find("/") % (len(s) + 1)]
abc
>>> print t[:t.find("/") % (len(t) + 1)]
no slash here
>>> print s[:s.rfind("/") % (len(s) + 1)]
abc/def
>>> print t[:t.rfind("/") % (len(t) + 1)]
no slash here
>>> print s[s.find("/")+1:]
def/ghi
>>> print t[t.find("/")+1:]
no slash here
>>> print s[s.rfind("/")+1:]
ghi
>>> print t[t.rfind("/")+1:]
no slash here

Ksh:

$ s='abc/def/ghi'
$ echo ${s%%/*}
abc
$ echo ${s%/*}
abc/def
$ echo ${s#*/}
def/ghi
$ echo ${s##*/}
ghi

Python:

>>> s='abc/def/ghi'
>>> print s[:s.find("/")]
abc
>>> print s[:s.rfind("/")]
abc/def
>>> print s[s.find("/")+1:]
def/ghi
>>> print s[s.rfind("/")+1:]
ghi

Edit:

To handle the case in which the pattern is missing, as pointed out by ΤΖΩΤΖΙΟΥ:

>>> s='abc/def/ghi'
>>> t='no slash here'
>>> print s[:s.find("/") % (len(s) + 1)]
abc
>>> print t[:t.find("/") % (len(t) + 1)]
no slash here
>>> print s[:s.rfind("/") % (len(s) + 1)]
abc/def
>>> print t[:t.rfind("/") % (len(t) + 1)]
no slash here
>>> print s[s.find("/")+1:]
def/ghi
>>> print t[t.find("/")+1:]
no slash here
>>> print s[s.rfind("/")+1:]
ghi
>>> print t[t.rfind("/")+1:]
no slash here
梨涡少年 2024-09-16 11:18:01
${name##*/}

相当于:

re.match(".*?([^/]*)$")[1]

${name%/*}

相当于:

re.match("(.*?)[^/]*$")[1]
${name##*/}

Is equivalent to:

re.match(".*?([^/]*)$")[1]

${name%/*}

Is equivalent to:

re.match("(.*?)[^/]*$")[1]
嘿哥们儿 2024-09-16 11:18:01

“向左剥离”、“向右剥离”等没有特殊状态。一种通用方法是 re.sub —— 例如,“将所有内容剥离到最后一个”包含斜杠”(ksh 概念化的“向左”):

name = re.sub(r'(.*/)(.*)', r'\2', name)

并删除“最后一个斜杠及其后面的所有内容”(每个 ksh“向右”):

name = re.sub(r'(.*)/.*', r'\1', name)

这些尽可能匹配,因为 * 是贪婪的;使用 *? 代替非贪婪匹配(“尽可能少”)。

There is no special status for "strip to the left", "strip to the right", etc. The one general method is re.sub -- for example, to "strip everything up to the last slash included" ("to the left" as ksh conceptualizes it):

name = re.sub(r'(.*/)(.*)', r'\2', name)

and to strip "the last slash and everything following" ("to the right" per ksh):

name = re.sub(r'(.*)/.*', r'\1', name)

These match as much as possible because the * in RE patterns is greedy; use *? instead for non-greedy matching ("as little as possible") instead.

苍白女子 2024-09-16 11:18:01
>>> def strip_upto_max(astring, pattern):
    "${astring##*pattern}"
    return astring.rpartition(pattern)[2]
>>> def strip_from_max(astring, pattern):
    "${astring%%pattern*}"
    return astring.partition(pattern)[0]
>>> def strip_upto(astring, pattern):
    "${astring#*pattern}"
    return astring.partition(pattern)[2]
>>> def strip_from(astring, pattern):
    "${astring%pattern*}"
    return astring.rpartition(pattern)[0]

>>> strip_from("hello there", " t")
'hello'
>>> strip_upto("hello there", " t")
'here'

>>> text= "left/middle/right"
>>> strip_from(text, "/")
'left/middle'
>>> strip_upto(text, "/")
'middle/right'
>>> strip_upto_max(text, "/")
'right'
>>> strip_from_max(text, "/")
'left'

但如果您的目的是将其与路径一起使用,请检查 是否os.path.dirname (${name%/*}) 和 os.path.basename (${name##*/}) 函数具有您需要的功能。

>>> def strip_upto_max(astring, pattern):
    "${astring##*pattern}"
    return astring.rpartition(pattern)[2]
>>> def strip_from_max(astring, pattern):
    "${astring%%pattern*}"
    return astring.partition(pattern)[0]
>>> def strip_upto(astring, pattern):
    "${astring#*pattern}"
    return astring.partition(pattern)[2]
>>> def strip_from(astring, pattern):
    "${astring%pattern*}"
    return astring.rpartition(pattern)[0]

>>> strip_from("hello there", " t")
'hello'
>>> strip_upto("hello there", " t")
'here'

>>> text= "left/middle/right"
>>> strip_from(text, "/")
'left/middle'
>>> strip_upto(text, "/")
'middle/right'
>>> strip_upto_max(text, "/")
'right'
>>> strip_from_max(text, "/")
'left'

But if your intention is to use it with paths, check whether the os.path.dirname (${name%/*}) and os.path.basename (${name##*/}) functions have the functionality you require.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文