首页 > 解决方案 > 正则表达式在 ios 上提取捆绑 ID

问题描述

我正在尝试使用以下正则表达式从 ios 中提取捆绑 ID:

^\w+(\.\w+)+

它适用于大多数情况,但在其他情况下失败。

例如使用以下包名称:

com.walmartmexico.WalmartMG
com.adobe.Adobe-Reader
com.instacart
gnustos.my.app.com.foo89.app.my.long.package.name.is.very.long
org.freevpn.vpn
com.adobe.scan.ios
com.adobe.Adobe-Reader
fontskeyboard.fonts
co.9count.wink
biser.stories
com.ephedra-software.StackOv
de.barbaraherold.freiheit

它没有完全捕获那些有连字符的:

com.ephedra-software.StackOv
com.adobe.Adobe-Reader

任何想法如何更改我的上述正则表达式以包含上述内容。

标签: regex

解决方案


我会使用:

^\w+(?:[.-]\w+)*$

演示

解释:

^                from the start of the input
    \w+          match one or more word characters
    (?:          followed by
        [.-]     dot or hyphen separator
        \w+      one or more word characters
    )*           subsequent terms zero or more times
$                end of the input

推荐阅读