wibeasley / class-regex-2015

material for our regex class
MIT License
1 stars 0 forks source link

Exercise - Regular Expressions #9

Closed andkov closed 6 years ago

andkov commented 6 years ago

@wibeasley , just FYI. I"m collecting bits and pieces.

Given the subject

HSDA-53
HSDA-52
HSDA-99
LHA-001
LHA-029
LHA-028
LHA-030
     BC
  HA-01
HSDA-11
  HA-99
HSDA-12

compose a regex that would separate the two components of the complex string, but also would match strings that do not have a numeric qualifier (BC). In other words, make the second capturing group optional.

To get you startered at regex101.com:

(\w+)-(\d+)
wibeasley commented 6 years ago

@andkov, I assume this is in R? If you start the R code (like put the subject in an array), I'll use that next time. Those options (eg, 'gm') might need to be modified if you're matching in R. I'd need to see if each line was its own element in an array. Or if this whole block was in a single element.

pattern 1a: ^\s*(\w{2,4})\s*-?\s*(\d{2,3})?$ pattern 1b: ^\s*(\w{2,4})\s*-?\s*(|\d{2,3})$ substitution: [\1]-{\2}

image