如何对字符串进行子串化?

发布于 2024-10-07 03:19:17 字数 283 浏览 0 评论 0原文

我有一个字符串“MenuItem {开源}”

如何从我的字符串中获取字符串 Open source

例如,

str1 = "MenuItem {Open source}"

执行一些操作将字符串二设置为...

print str2  # 'Open source'

我如何使用 python 或 jython 实现此目的?

I have a string "MenuItem {Open source}".

How can I get the string Open source from my string?

e.g.

str1 = "MenuItem {Open source}"

perform some actions to set string two to be...

print str2  # 'Open source'

How can I acheive this using python or jython?

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

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

发布评论

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

评论(3

守不住的情 2024-10-14 03:19:17

您可以使用正则表达式获取它。

>>> import re
>>> str1 = "MenuItem {Open source}"
>>> re.search(r'{(.*)}', str1).group(1)
'Open source'

您还可以通过在 {} 分隔符上拆分字符串来获取它(这里我使用 str.rsplit 而不是 str. split 以确保它在最右侧的匹配处拆分):

>>> str1.rsplit('{')[1].rsplit('}')[0]
'Open source'

You can get it with a regular expression.

>>> import re
>>> str1 = "MenuItem {Open source}"
>>> re.search(r'{(.*)}', str1).group(1)
'Open source'

You can also get it by splitting the string on the { and } delimiters (here I use str.rsplit rather than str.split to make sure it splits at the right-most match):

>>> str1.rsplit('{')[1].rsplit('}')[0]
'Open source'
2024-10-14 03:19:17

提取子字符串:Python 中的字符串可以像数组一样加下标:s[4] = 'a'。与 IDL 一样,索引可以用切片表示法指定,即两个索引用冒号分隔。这将返回一个包含字符index1到index2-1的子字符串。索引也可以为负数,在这种情况下它们从右侧开始计数,即-1 是最后一个字符。因此,可以像“因此在您的示例中”那样提取子字符串

  s = "Go Gators! Come on Gators!"

  x = s[3:9] #x = "Gators"
  x = s[:2] #x = "Go"
  x = s[19:] #x = "Gators!"
  x = s[-7:-2] #x = "Gator"

,您需要 str2 = str1[10:21] = "Open Source"

当然,这是假设它始终是 Open Source 和 MenuItem...

相反,您可以使用 find

int find(sub [,start[,end]])

返回 sub 在字符串中第一次出现的数字位置。如果未找到 sub,则返回 -1。

  x = s.find("Gator") #x = 3
  x = s.find("gator") #x = -1

因此,您可以使用 str1.find("{") 获取第一个大括号,使用 str1.find("}") 获取第二个大括号。

或者在一个:

str2 = str1[str1.find("{"):str1.find("}")]

未经测试的代码中,您可能需要在某处添加+1,但理论上应该可行,或者至少让您走上正确的轨道;)

Extracting substrings: Strings in Python can be subscripted just like an array: s[4] = 'a'. Like in IDL, indices can be specified with slice notation i.e., two indices separated by a colon. This will return a substring containing characters index1 through index2-1. Indices can also be negative, in which case they count from the right, i.e. -1 is the last character. Thus substrings can be extracted like

  s = "Go Gators! Come on Gators!"

  x = s[3:9] #x = "Gators"
  x = s[:2] #x = "Go"
  x = s[19:] #x = "Gators!"
  x = s[-7:-2] #x = "Gator"

Therefore in your examplel, you'll need str2 = str1[10:21] = "Open Source".

Of course, that's assuming it's always Open Source and MenuItem...

Instead, you can use find:

int find(sub [,start[,end]])

returns the numeric position of the first occurance of sub in the string. Returns -1 if sub is not found.

  x = s.find("Gator") #x = 3
  x = s.find("gator") #x = -1

So you can use str1.find("{") to get the first brace, and str1.find("}") to get the second.

or in one:

str2 = str1[str1.find("{"):str1.find("}")]

untested code, you may need to add a +1 somewhere, but in theory that should work, or at least get you on the right track ;)

拔了角的鹿 2024-10-14 03:19:17
var1 = "MenuItem {Open source}"
var2 = var1.find('{')
var3 = var1.find('}')
ans = var1[var2+1: var3]
print ans

你就看到了字符串“开源”!

var1 = "MenuItem {Open source}"
var2 = var1.find('{')
var3 = var1.find('}')
ans = var1[var2+1: var3]
print ans

There you are with the string "Open source"!!!

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