从字符串中提取用户名

发布于 2025-02-09 07:25:32 字数 235 浏览 1 评论 0 原文

我有一个字符串名称,可以包括以下可能性,我希望使用Regex表达式以相应的下划线删除字符串的X部分,并仅获取文件名。

字符串的可能性名称

  1. XXX_XXX_FILENAME
  2. XXX_FILENAME

请注意,X是大写,仅由字母{Az}组成。我还能有一个以下的正则表达式,也可以用下划线删除X零件吗?

I have a string name that can include the following possiblities and I wish to use a regex expression to remove that X part of the string with its corresponding underscore and obtain the filename only.

Possibilities of string name:

  1. XXX_XXX_filename
  2. XXX_filename

kindly note that X is an uppercase and consists of alphabets {A-Z} only. Can i have a regex that removes the X parts with the underscore as well?

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

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

发布评论

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

评论(2

八巷 2025-02-16 07:25:32

您可以重复1或2次匹配1或更多大写字符[AZ],然后进行下划线。

在替换中使用一个空字符串。

(?:[A-Z]+_){1,2}

You can repeat 1 or 2 times matching 1 or more uppercase chars [A-Z] followed by an underscore.

In the replacement use an empty string.

(?:[A-Z]+_){1,2}

Regex demo

丶视觉 2025-02-16 07:25:32

您可以尝试以下操作:

“ xxx_xxx_filename” .replace(/^[a-z _]*/g,“”)

如果您知道可以限制Regexp的总字符数:

<代码>“ xxx_xxx_filename” .replace(/^[a-z _] {3,7}/g,“”)

如果您也知道上限的组总数,则可以使用此ALOS:

“ xxx_xxx_filename” .replace(/^([az]+_??){0,2}/g,“”)

You can try this:

"XXX_XXX_filename".replace(/^[A-Z_]*/g,"")

If you know the number of total characters you can restrict the regexp a bit more:

"XXX_XXX_filename".replace(/^[A-Z_]{3,7}/g,"")

If you also know the total number of groups for upper case you could use this alos:

"XXX_XXX_Filename".replace(/^([A-Z]+_?){0,2}/g,"")

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