Session Ready
Exercise

Extracting age and gender again

You can now also take another look at your pattern for pulling out age and gender from the injury narratives. In the last chapter you took two steps to extract the pattern and then pull it apart. Take a go at doing it in one step!

Instructions 1/2
undefined XP
  • 1

    We've written out your whole pattern here rather than use the pattern variables you created, because you'll need to capture pieces that were inside those patterns.

    • Add capture() to your pattern to pull out the age, unit and gender parts of the pattern.
    • Use str_match() to pull them out of narratives, and take a look.
    • 2
      • That's pretty good, but it might be nice just to capture the first letter of the units, e.g. the Y or M. Alter your pattern to capture a Y or M, and then optional()ly match O or R.
      • Check your new pattern still picks out the right pieces with str_view().
      • Pull out the pieces with str_match().