Extract a string from a field using complex rules

Jubinell

Board Regular
Joined
Jan 17, 2008
Messages
166
Hi,

I have a dictionary table with a definition text field. This field is of the following format:

(c-1) (c-2) ... (c-nth) definition

Each line always starts with at least one category keyword in parentheses, separated by a single space, followed the the definition of the word.
For example:

(n) (comp) restart, reboot
(n, v) to chase
(adj) (seemingly) defeatable
(exp) to verify (a statement), to warrant

As you can see, the problem here is that the definition may or may not contain texts with parentheses and the latter may or may not be separated from the rest of the definition with a space.

Objective:
I would like to create new fields, category1, category2, etc. to capture the category keywords in parentheses associated with each word entry. For simplicity's sake let's assume that the definitions do NOT start with some text in a parenthesis (although I'd appreciated your thoughts on how to tackle this problem as well), my query/macro will return the following results for the four definitions above:

cat1 | cat2 | cat3
n | comp
n, v
adj | seemingly
exp

Any ideas?
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Here's one approach:
This code goes in a new module (Alt+F11, Insert>Module, then Paste the code and save the module as basParse).

Code:
Function ParseTerms(strIn As String, intPart As Integer)
    Dim strText As String
    Dim intLen As Integer
    Dim x
    Dim i As Integer
    
    'grab just the elements we need.
    strText = Replace(strIn, ") (", "|")
    strText = Left(strText, InStr(1, strText, ")") - 1)
    strText = Mid(strText, 2)
    
    'separate out the categories
    x = Split(strText, "|")
    If intPart > UBound(x) + 1 Then
        ParseTerms = ""
    Else
        ParseTerms = x(intPart - 1)
    End If
End Function

Assuming that you have tblDictionary with a field called Term, you would extract the first term using an expression like
Cat1:ParseTerms([Term],1)

Repeat for as many terms as you think you will have. I did a test using 4 Cat fields -- result is:
Excel Workbook
ABCDE
10TermCat1Cat2Cat3Cat4
11(n) (comp) restart, rebootncomp
12(n, v) to chasen, v
13(adj) (seemingly) defeatableadjseemingly
14(exp) to verify (a statement), to warrantexp
Sheet


Denis
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,462
Members
448,965
Latest member
grijken

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top