douxieshang5577 2017-01-04 16:39
浏览 892

Golang regExp用于将字符匹配到一个字符串(不包括子字符串)

I have a use case: Need to match the following . Input string: abpcdcccddd Matching criteria: match all characters starting with 'a' and ending with 'ccc' Example: abpcdccc ( single character 'c' in 4th Poistion got successfully ignored in matching)

Can you help me with the Golang regular expression for the same?

  • 写回答

2条回答 默认 最新

  • drd94483 2017-01-04 16:45
    关注

    I'm not sure that I understand exactly what it is you are wanting, but I can probably get you on the right track.

    If you want to match a string that has an a, followed by some word characters, followed by ccc, then you can simply use something like this:

    a\w+ccc
    

    If you want the string to start and end with a and ccc respectively, you can do something like this:

    ^a\w+ccc$
    

    If you want to only allow specific characters like lowercase letters, you can put them into a character class like this:

    a[a-z]+ccc
    

    Hopefully one of those answers your question.

    评论

报告相同问题?