Skip to content

Instantly share code, notes, and snippets.

@librasteve
Last active July 24, 2023 21:08
Show Gist options
  • Save librasteve/c3164d3744950c4370c1560b52bdae34 to your computer and use it in GitHub Desktop.
Save librasteve/c3164d3744950c4370c1560b52bdae34 to your computer and use it in GitHub Desktop.
measure-regex.raku
use Physics::Measure :ALL;
$Physics::Measure::number-comma = '';
my @lines = q:to/END/;
say &fs('car in USA highway');
# The average speed of cars on US highways is approximately 65 mph.
say &fs('rocket leaving Earth');
#The average speed of a rocket leaving Earth is approximately 7.8 km/s.
say &fs('rocket leaving Earth in miles per hour');
# The average speed of a rocket leaving Earth is approximately 25,000 mph.
END
for @lines.split("\n").grep( / \# / ) -> $line {
$line ~~ / 'is approximately ' (.*?) '.' $/;
say ♎️"$0";
}
say '--';
my regex measure {
.+ #grab chars
<?{ ♎️"$/" ~~ Measure }> #assert coerces via '♎️' to Measure
}
for @lines.split("\n").grep( / \# / ) -> $line is copy {
$line .= chop;
$line ~~ / 'is approximately ' <measure> $/;
say ♎️"$/<measure>";
}
@librasteve
Copy link
Author

librasteve commented Jul 24, 2023

65mph
7.8km/s
25000mph
--
65mph
7.8km/s
25000mph

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment