VBscript_Words containing unique and case-insensitive letters only

Viewed 176

I have a large text file in which I need to find the words containing only unique letters(a-z,A-Z). The words should not contain any character other than letters. Also, it needs to be case-insensitive so that words like alphA, morNing are not matched.

Examples:

 marco        -  Should match(because of unique letters)
 asia         -  Should Not Match(contains 2 'a')
 asiA         -  Should Not Match(as it has 'a' and 'A')
 alpha        -  Should not match
 mike         -  Should match
 roger        -  Should not match
 abascus      -  Should not match
 mach1        -  Should not match(because of presence of 1)    

Sample text from file against which I need to test:

The shares together form stock.The stock of a corporation is partitioned into shares, the total of which are stated at the time of business formation. Additional shares may subsequently be authorized by the existing shareholders and issued by the company. In some jurisdictions, each share of stock has a certain declared par value, which is a nominal accounting value used to represent the equity on the balance sheet of the corporation. In other jurisdictions, however, shares of stock may be issued without associated par value.

Shares represent a fraction of ownership in a business. A business may declare different types (or classes) of shares, each having distinctive ownership rules, privileges, or share values. Ownership of shares may be documented by issuance of a stock certificate. A stock certificate is a legal document that specifies the number of shares owned by the shareholder, and other specifics of the shares, such as the par value, if any, or the class of the shares.

In the United Kingdom, Republic of Ireland, South Africa, and Australia, stock can also refer to completely different financial instruments such as government bonds or, less commonly, to all kinds of marketable securities.

My attempt:

\b(?![^a-zA-Z]+)(?!(?:[a-zA-Z]*([a-zA-Z]))*\1)[a-zA-Z]+\b

but it is not able to match anything here.

I have been stuck here for quite sometime. Please point me in right direction. Thanks

1 Answers

Try this regex:

\b(?![^a-zA-Z]+\b)(?![a-zA-Z]*([a-zA-Z])[a-zA-Z]*\1)[a-zA-Z]+\b

Click for Demo

Explanation:

  • \b - Word boundary
  • (?![^a-zA-Z]+\b) - Negative lookahead validating that words should only contain the 1+ letters
  • (?![a-zA-Z]*([a-zA-Z])[a-zA-Z]*\1) - Another negative lookahead - this part is for validating no 2 letters are repeated. Further break-up below:
    • [a-zA-Z]* - checks for the presence of 0+ letters
    • ([a-zA-Z]) - captures a letter in a group. This letter captured in the group will be checked for any repetition.
    • [a-zA-Z]* - checks for the presence of 0+ letters again so as to consider for the cases when the repeated letters are not next to each other.
    • \1 - checks for the letter captured captured in group1
  • [a-zA-Z]+ - matches 1+ occurrences of a letter
  • \b - Word Boundary

VBScript Code:

Option Explicit
Dim objRE, strTest, objMatches, match, strOutput
strTest = "marco asia asiA alpha mike roger abascus mach1"
Set objRE = New RegExp
objRE.Global=True
objRE.IgnoreCase=True
objRE.Pattern="\b(?![^a-zA-Z]+\b)(?![a-zA-Z]*([a-zA-Z])[a-zA-Z]*\1)[a-zA-Z]+\b"
Set objMatches = objRE.Execute(strTest)
For Each match In objMatches
    strOutput = strOutput & match.Value & vbCrLf
Next
MsgBox strOutput
Set objMatches = Nothing
Set objRE = Nothing

Output:

enter image description here

Related