regex - Regular expression to find position of the last alpha character that is followed by a space? -
i using coldfusion 10. need use regular expression , need help.
i have lengthy content (up 8,000 characters) , want create teaser. after length (which define elsewhere), want find last alpha character followed space. remove after character. add ellipsis (...)
mystring = "the lazy brown fox not dog."
in case, delete after "a" precedes "dog".
mystring = "there 123 boxes on hill, says 612 guy."
in case, delete after "that" precedes "612 ".
mystring = "i fell down stairs on june 30th, 1962."
in case, delete after "june" precedes "30th".
what regular expression use find position of last alpha [a-z] character followed space?
myreg = ""; lastposition = refindnocase(myreg, mystring);
i'm not sure refindnocase, think can try rereplacenocase. hope cf can take references regex engines do:
rereplacenocase(mystring, "(.*\b[a-za-z]+\b)\s.*", "$1", all);
edit: backreference, appears use backslash instead of dollar sign:
rereplacenocase(mystring, "(.*\b[a-za-z]+\b)\s.*", "\1", all);
and if goes well, should have this.
.*
matches besides newline character, \b
matches word boundaries, [a-za-z]+
alphabet characters , \s
space after it.
the greediness of first .*
's being exploited here capture as possible until last word followed space.
and guess can add ellpses after $1
so:
rereplacenocase(mystring, "(.*\b[a-za-z]+\b)\s.*", "\1 (...)", all)
if want use refind()
, maybe use this:
refindnocase("[a-za-z](?:\s\d+|\w+,)*\s[^\s]+\.$", mystring);
note haven't tested against other possible scenarios, tried few don't work above one:
refindnocase("[a-za-z](?:\s\d+|\s?\w+[,.-]+)*\s[^\s]+[.\s]*$", mystring);
and few test subjects: link.
refind
give position of last alpha character. can add 1 position of space in original string.
Comments
Post a Comment