I can't include ' symbol to Regular Expressions

后端 未结 1 1525
隐瞒了意图╮
隐瞒了意图╮ 2021-01-21 06:19

I try to include \' symbol to Regular Expressions

I use this function

func matches(for regex: String, in text: String) -> [String] {

    do {
               


        
1条回答
  •  无人及你
    2021-01-21 07:14

    I guess that what you really want is this:

    "['0-9a-zA-Z]+"
    

    Note that I have removed the ^ (text start) and $ (text end) characters because then your whole text would have to match.

    I have merged the groups because otherwise you would not match the text as a whole word. You would get separate apostrophe and then the word.

    I have changed the character into the proper ' character. The automatic conversion from the simple apostrophe is caused by iOS 11 Smart Punctuation. You can turn it off on an input using:

    input.smartQuotesType = .no
    

    See https://developer.apple.com/documentation/uikit/uitextinputtraits/2865931-smartquotestype

    0 讨论(0)
提交回复
热议问题