Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

regex to match a word with unique (non-repeating) characters

I'm looking for a regex that will match a word only if all its characters are unique, meaning, every character in the word appears only once.

Example:
abcdefg -> will return MATCH
abcdefgbh -> will return NO MATCH (because the letter b repeats more than once)

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this, it might work,

^(?:([A-Za-z])(?!.*1))*$

Explanation

Assert position at the beginning of a line (at beginning of the string or after a line break character) ?^?
Match the regular expression below ?(?:([A-Z])(?!.*1))*?
   Between zero and unlimited times, as many times as possible, giving back as needed (greedy) ?*?
   Match the regular expression below and capture its match into backreference number 1 ?([A-Z])?
      Match a single character in the range between “A” and “Z” ?[A-Z]?
   Assert that it is impossible to match the regex below starting at this position (negative lookahead) ?(?!.*1)?
      Match any single character that is not a line break character ?.*?
         Between zero and unlimited times, as many times as possible, giving back as needed (greedy) ?*?
      Match the same text as most recently matched by capturing group number 1 ?1?
Assert position at the end of a line (at the end of the string or before a line break character) ?$?

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...