Help me with the logic on this one. == instead of && in Expression

I have a column of data with this format:

Company Name (1234)
Company Name (6789)

I want to move the system ID code values between '(' and ')' to another column.

I expected this to work:

if(value.contains("(") && value.contains(")"), value.split("(")[1].split(")")[0], null)

But I got a parsing error in regards to '&&'

I replaced '&&' with '==' and it worked.

I'm new to these expressions, but I really felt '&&' would be correct. Am I missing something?

OpenRefine is using a more functional approach in GREL. Meaning instead of && and || you have functions like and and or.

Meaning your expression would be written as:

if(and(value.contains("("), value.contains(")")), value.split("(")[1].split(")")[0], null)

Or using regular expressions:

value.find(/(?<=\()[^)]+(?=\))/)[0]

Yes regular expressions are great for this kind of thing, though they take a bit of getting used to if you haven't used them before. @b2m 's version is good, but maybe using the match function is slightly simpler:

value.match(/.*\((\d+)\).*/)[0]

This actually is similar to the example in GREL functions | OpenRefine . If you don't care about whether the number has brackets around it, you can just do

value.find(/\d+/)[0]

which picks out the first set of consecutive digits in the value. The regular expression is bound by the / symbols, the \d means a digit, and the + means 'one or more occurrances of'. The [0] at the end just means to take the first instance of the match, since the find function will create an array of all matches it finds (so you could do [-1] if you wanted the last instance).

2 Likes

IF (but only if) all values in your column follow the pattern you've laid out here, and none of the company names include parentheses, then another approach would be to use the Edit columns -> Split into several columns menu option (in the drop down of the column)

Using a regular expression separator of [()] (as illustrated) will put the numbers into their own column

1 Like