0

Here is a piece of code that works as I expect it to:

"\\bwork(s?|(?:ing)?)\\s?(?:from)?\\s?home\\b"

it makes the space optional after tele. However, when I try to edit it such that it also includes hyphen(-) by using

"\\bwork(s?|(?:ing)?)([\\s-])?(?:from)?([\\s-])?home\\b"

it stops picking up the matches with spaces too. An example where the first works, but the second does not is This is work from home, on-call position. What am I doing wrong here? I have a feeling it is something very basic, but I am new to regular expressions and any help would be appreciated.

  • 1
    did you try `tele[ -]?` instead – rawr Jul 23 '20 at 19:43
  • 1
    You don't need to escape a hyphen in a character class. Just put it first or last so that it doesn't denote a range. Assuming you won't be referencing the content of the capture group `(ing)?` (for example) you could use a non-capture group instead: `(?:ing)?`. Both work but the former may lead readers to waste time considering whether you are referencing that group somewhere. It's particularly confusing if you are referencing some capture groups but not others. – Cary Swoveland Jul 23 '20 at 19:53
  • @CarySwoveland I did try it without escaping the hyphen, but it does not work any differently. Thanks for the note on the non-capturing group, I am sure it will make my life easier in the future! – Pratyush T. Jul 23 '20 at 20:03
  • Can you add an example to the question where it does work and when it stops working? – The fourth bird Jul 23 '20 at 20:08
  • @Thefourthbird edited and added an example – Pratyush T. Jul 23 '20 at 20:39
  • 1
    @PratyushT. I think you do get the matches. See https://regex101.com/r/jC67My/1 and https://regex101.com/r/e8Ihni/1 – The fourth bird Jul 23 '20 at 20:43
  • @rawr ```tele[ -]?``` does seem to work. Do you have any idea why using ```\s``` change the result? – Pratyush T. Jul 23 '20 at 21:06
  • 1
    You just need to use `perl=TRUE` in your command, and use `[\\s-]`. Else, use `[[:space:]-]` in a TRE regex. – Wiktor Stribiżew Jul 23 '20 at 21:06
  • @Thefourthbird that is really strange. it must be an artifact of the command that I am using in R - ```regexpr``` – Pratyush T. Jul 23 '20 at 21:06
  • 1
    Right, you are using base R, and without `perl=TRUE`, these functions use the TRE regex library, and you can't use a Perl-like shorthand character class inside a bracket expression. – Wiktor Stribiżew Jul 23 '20 at 21:09
  • @WiktorStribiżew Thanks for this, that explains it. Till I get the time to properly go through the documentation on this, I better be careful with this. – Pratyush T. Jul 23 '20 at 21:30

0 Answers0