I am parsing some text line by line and if a given line ends with any punctuation or a number I’d like to have a boolean return true.
Is regex the best way or should I iterate with an array of chars to match? Feels like the array would be far too big and costly…
Could someone help me with the regex if that is indeed a good way?
function ends_with_punctuation_or_num($string){
// check if the string ends with punctuation or a number
if (/* TODO */)
return true;
else
return false;
}
Put this into your if-check:
That will match a digit, or any of the given punctuation characters right before the end of the string (
$). Add any other punctuation characters you want to regard as a match into the character class (the square brackets).