regex - 查找符号集前面的单词

标签 regex swift

如何找到 [¹²³⁴⁵⁶⁷⁸⁹⁰] 之前的单词。例如:

let myString = "Regular expressions¹ consist of constants, ² and operator symbols...³"

请提供一个模式来选择从目标词开头到上标的字符:

"expressions¹", "constants, ²", "symbols...³"

& pattern 只选择目标词

"expressions", "constants", "symbols"

最佳答案

这将匹配您的示例。

代码点:

\b\w+\W*[\x{B9}\x{B2}\x{B3}\x{2074}\x{2075}\x{2076}\x{2077}\x{2078}\x{2079}\x{2070}]+

来自维基百科:

The most common superscript digits (1, 2, and 3) were in ISO-8859-1 and were therefore carried over into those positions in the Latin-1 range of Unicode. The rest were placed in a dedicated section of Unicode at U+2070 to U+209F.

更新:

要获得以单词或非单词开头的单独 block ,您可以
从非词类中排除上标范围。
正则表达式更长且更冗余,但它有效。

(?:\b\w+[^\w\x{B9}\x{B2}\x{B3}\x{2074}\x{2075}\x{2076}\x{ 2077}\x{2078}\x{2079}\x{2070}]*|[^\w\x{B9}\x{B2}\x{B3}\x{2074}\x{2075}\x{2076}\x{2077}\x{2078}\x{2079}\x{2070}]+)[\x{B9}\x{B2}\x{B3}\x{2074}\x {2075}\x{2076}\x{2077}\x{2078}\x{2079}\x{2070}]+

格式化

 (?:
      \b 
      # Required - Words
      \w+ 
      # Optional - Not words, nor supersctipt
      [^\w\x{B9}\x{B2}\x{B3}\x{2074}\x{2075}\x{2076}\x{2077}\x{2078}\x{2079}\x{2070}]* 

   |  # or,

      # Required - Not words, nor supersctipt
      [^\w\x{B9}\x{B2}\x{B3}\x{2074}\x{2075}\x{2076}\x{2077}\x{2078}\x{2079}\x{2070}]+ 
 )
 # Required - Superscript
 [\x{B9}\x{B2}\x{B3}\x{2074}\x{2075}\x{2076}\x{2077}\x{2078}\x{2079}\x{2070}]+ 

关于regex - 查找符号集前面的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33875363/

相关文章:

java - .split() 和 [\\W] 创建一个额外的空字符串?

c# - 伪 IPv4 正则表达式

javascript - 正则表达式匹配卡代码输入

ios - 无法在 iPhone 中获取密码 UItextfield,但在模拟器中可以

ios - 如何使用 setObject Swift 2.0 实现基于时间的 NSCache

java - 正则表达式引起的错误?

regex - 有什么好的技巧可以帮助记住 RegEx?

ios - 画一个半圆按钮iOS

swift - 在 for 循环中处理负索引的优雅 Swift 方法

ios - 检查 ios 中的位置