You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从带前导空格的文件路径中提取带点的扩展名?

Hey there! Let's figure out how to pull those file extensions (including the leading dot) from your given file paths. I've got a couple of solid approaches for you, plus the exact results for your specific examples:

Approach 1: Simple String Manipulation

The most straightforward way is to target the last occurrence of a dot (.) in the full path, then grab everything from that dot to the end of the string. This works because valid file extensions always come after the final dot in a filename—even if there are dots earlier in directory names (like com.c.rb.gold... or cbmo-thgcb-ext-gm.war).

For your three paths, here's what you'd extract:

  • Path 1: (_OasvkDSOEee_ruCXphIMsQ) /com.c.rb.gold.gbl.fw.proxy.component_jar/src/main/java/com/roup/ebus/mobile/api/common/resource/APIProxyResource.java
    Extracted extension: .java
  • Path 2: (_7ZgAUO-qEeeFqO9kl3sUYw) /cbmo-thgcb-ext-gm.war/src/main/app/WEB-INF/classes/rules/THMBK/APIRequestResponseMapper.xml
    Extracted extension: .xml
  • Path 3: (_TM6vEFKjEee-NMziq4x8wA) /com.citi.rb.gold.memfis.sb.war/src/main/webapp/citibank/eclipselite/bank/memfis/sb/maintenance/SBBondCalculatorPopup.jsp
    Extracted extension: .jsp

Approach 2: Regular Expressions

If you prefer using regex, you can use a pattern that specifically matches the final dot and all characters after it. The pattern \.[^.]*$ does exactly this:

  • \.: Matches the literal dot
  • [^.]*: Matches any number of characters that aren't dots
  • $: Ensures we're matching from the end of the string

Here's a quick Python example to demonstrate:

import re

# Your list of paths
file_paths = [
    "(_OasvkDSOEee_ruCXphIMsQ) /com.c.rb.gold.gbl.fw.proxy.component_jar/src/main/java/com/roup/ebus/mobile/api/common/resource/APIProxyResource.java",
    "(_7ZgAUO-qEeeFqO9kl3sUYw) /cbmo-thgcb-ext-gm.war/src/main/app/WEB-INF/classes/rules/THMBK/APIRequestResponseMapper.xml",
    "(_TM6vEFKjEee-NMziq4x8wA) /com.citi.rb.gold.memfis.sb.war/src/main/webapp/citibank/eclipselite/bank/memfis/sb/maintenance/SBBondCalculatorPopup.jsp"
]

# Regex pattern to find the final extension
extension_pattern = r"\.[^.]*$"

for path in file_paths:
    match = re.search(extension_pattern, path)
    if match:
        print(f"Extracted extension: {match.group()}")

When you run this, you'll get:

Extracted extension: .java
Extracted extension: .xml
Extracted extension: .jsp

Quick Edge Case Note

If you ever encounter files with no extension (not the case here), the string method would either return an empty string or throw an error (depending on how you implement it), and the regex would return None—so just make sure to add a check if you're dealing with mixed path types.

内容的提问来源于stack exchange,提问作者Franklin Francis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:16:33