3

I have a column of strings, and I want to use a regex to find commas or pipes in every cell, and then make an action. I tried this, but it doesn't work (no syntax error, just doesn't match neither commas nor pipes).

if(value.contains(/(,|\|)/), ...

The funny thing is that the same regex works with the same data in SublimeText. (Yes, I can work it there and then reimport, but I would like to understand what's the difference or what is my mistake).

I'm using Google Refine 2.5.

Aubrey
  • 507
  • 4
  • 20
  • Are you sure `value.contains` can accept a regex? Try `if(value.match(/.*[,|].*/)` – Wiktor Stribiżew Feb 23 '16 at 10:29
  • No, I'm not sure, but I tried both value.match and value.contains with your regex (and mine), and the behaviour is the same. I'm starting to think that maybe the issue is escaping the pipe: `/\|/`should find the pipe, but doesn't work. – Aubrey Feb 23 '16 at 10:40
  • 1
    Acc. to the [GREL cheatsheet](http://arcadiafalcone.net/GoogleRefineCheatSheets.pdf) `value.contains` supports regex and that escaping should work. Maybe the issue is somewhere else. – Wiktor Stribiżew Feb 23 '16 at 10:43
  • Well, now it seems that is in fact the regex that breaks everything. If I put "string" everything works, if I use regex doesn't. – Aubrey Feb 23 '16 at 11:23
  • 1
    Well, it seems that `match` only returns capture groups. Try `if(value.match(/.*([,|]).*/) != null)` or `if (value.partition(/.*[,|].*/) != null)` or something like that. See [this page](https://groups.google.com/forum/#!topic/openrefine-dev/sdXQKTpbY0k). – Wiktor Stribiżew Feb 23 '16 at 11:44
  • Thanks, the first one works. – Aubrey Feb 23 '16 at 12:05
  • 1
    I don't think that GREL cheatsheet is correct. Current documentation including the Wiki https://github.com/OpenRefine/OpenRefine/wiki/GREL-String-Functions and the help information within the application says that 'contains' accepts a string fragment, but does not mention regex – Owen Stephens Feb 23 '16 at 15:41
  • Yes. From what I understand, contains() doesn't support regex. – Aubrey Feb 23 '16 at 16:04

2 Answers2

2

You can use a combination of if and isNonBlank like:

if(isNonBlank(value.match(/your regex/), ...
Lara M.
  • 855
  • 2
  • 10
  • 23
2

Since value.match should return captured texts, you need to define a regex with a capture group and check if the result is not null.

Also, pay attention to the regex itself: the string should be matched in its entirety:

Attempts to match the string s in its entirety against the regex pattern p and returns an array of capture groups.

So, add .* before and after the pattern you are looking inside a larger string:

if(value.match(/.*([,|]).*/) != null)
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563