简体   繁体   English

正则表达式在字符串末尾的第二个“。”点字符后提取字符串

[英]Regex extract string after the second “.” dot character at the end of a string

I want to extract the second "file extension" of a file name so: 我想提取文件名的第二个“文件扩展名”,以便:

/Users/path/my-path/super.lol.wtf/main.config.js

What I need is: 我需要的是:

.config.js

What would be totally awesome if I got an array with 2 strings in return: 如果我得到一个包含2个字符串的数组,那将是多么令人敬畏:

var output = ['main', '.config.js'];

What I have tried: 我尝试过的:

^(\d+\.\d+\.)

But that ain't working. 但这不起作用。

Thanks 谢谢

You could use the following: 您可以使用以下内容:

([^\/.\s]+)(\.[^\/\s]+)$

Example Here 这里的例子

  • ([^\\/.\\s]+) - Capturing group excluding the characters / and . ([^\\/.\\s]+) - 捕获除字符/和之外的组. literally as well as any white space character(s) one or more times. 字面上以及任何空格字符一次或多次。

  • (\\.[^\\/\\s]+) - Similar to the expression above; (\\.[^\\/\\s]+) - 与上面的表达式相似; capturing group starting with the . 捕获组开始于. character literally; 字面上的字面; excluding the characters / and . 排除字符/. literally as well as any white space character(s) one or more times. 字面上以及任何空格字符一次或多次。

  • $ - End of a line $ - 结束


Alternatively, you could also use the following: 或者,您也可以使用以下内容:

(?:.*\/|^)([^\/.\s]+)(\.[^\/\s]+)$

Example Here 这里的例子

  • (?:.*\\/|^) - Same as the expression above, except this will narrow the match down to reduce the number of steps. (?:.*\\/|^) - 与上面的表达式相同,但这会缩小匹配范围以减少步数。 It's a non-capturing group that will either match the beginning of the line or at the / character. 它是一个非捕获组,它将匹配行的开头或/字符。

The first expression is shorter, but the second one has better performance. 第一个表达式更短,但第二个表达式具有更好的性能。

Both expressions would match the following: 两个表达式都匹配以下内容:

['main', '.config.js']

In each of the following: 在以下各项中:

/Users/path/my-path/some.other.ext/main.config.js
some.other.ext/main.config.js
main.config.js

Here is what you need: 这是你需要的:

(?:\/?(?:.*?\/)*)([^.]+)*\.([^.]+\.[^.]+)$
  • (?: means detect and ignore, (?:表示检测并忽略,
  • [^.]+ means anything except . [^.]+表示除了之外的任何内容.
  • .*? means pass until last / 意思是直到最后/

Check Here 检查一下

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM