The regex ^[^a-zA-Z]*([a-zA-Z]+)[^a-zA-Z]*$ should work.
^ matches beginning of the line
[^a-zA-Z]* matches zero or more occurrences of a non-letter
[a-zA-Z]+ matches one or more occurrences of a letter
[^a-zA-Z]* matches zero or more occurrences of a non-letter
$ matches end of the line
So, it will ignore leading and following non-letters in a line and only match if there's no non-letter between the first letter and last letter.
The parenthesis indicates a capture group, which is the part we want to extract and print. I originally wrote and tested this for .NET, but here's a sed command. Don't ask me how sed works, I have no idea.
sed -rn 's/^[^a-zA-Z]*([a-zA-Z]+)[^a-zA-Z]*$/\1/p' inputfile
Instead of printing, you can write directly to an output file:
sed -rn 's/^[^a-zA-Z]*([a-zA-Z]+)[^a-zA-Z]*$/\1/w outputfile' inputfile
Edwaandrd? – Ignacio Vazquez-Abrams May 5 '12 at 5:56Edwardentirely without a dictionary. – Dennis May 5 '12 at 5:57Edwa4rd" and e.g. "Lisa7anna" without knowing all valid names beforehand in some dictionary. And is "Mary0anne" one invalid (Maryanne) or two valid (Mary, Anne) names? That's the problem that the earlier commenters pointed at. – Daniel Andersson May 5 '12 at 12:13