[SOLVED] How to exclude all speacial characters using regex?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Show your own efforts when posting, and do basic research. After three years, you should have SOME scripting/research skills.
Putting "bash regex strip out anything but letters and numbers" into Google pulls up a LOT of 'hints'. You've been told many times to post things in CODE tags, but don't seem to follow that advice either. The [:alnum:] is alpha-numeric.
Show your own efforts when posting, and do basic research. After three years, you should have SOME scripting/research skills.
Putting "bash regex strip out anything but letters and numbers" into Google pulls up a LOT of 'hints'. You've been told many times to post things in CODE tags, but don't seem to follow that advice either. The [:alnum:] is alpha-numeric.
I understand and I am definitely trying to get the answer and of course everyone first tries google which I also did and if that didnt resolve then come here.
You need to escape certain characters inside the RegEx:
Code:
while read -r line;do
if [[ ! "$line" =~ [][()\'\"~!\`@/?\>\<\\] ]];then
echo "$line"
fi
done < "/path/to/file"
The above code takes care of the most problematic ones. Notice, that if you want to match a literal ']' inside the brackets then it must be the first character after the opening '['.
I will leave matching the remaining characters as an excercise.
PS:
You can also achieve this by using [:alnum:] by TB0ne but it has also a pitfall. I think, however, that doing it the "hard" way is more educational in the long run since you can learn how to handle certain characters in a RegEx.
Last edited by crts; 07-18-2019 at 08:54 AM.
Reason: Added PS
I understand and I am definitely trying to get the answer and of course everyone first tries google which I also did and if that didnt resolve then come here. Will definitely ensure to follow the code tags.
Sorry, just don't believe that. Putting the search term I used into Google yielded 559,000 hits....hard to believe that out of all that there wasn't one 'hint' you could have used. And you've been asked about CODE tags for a LONG time, but don't use them.
Quote:
Originally Posted by blason
Code:
'[!@#%%$^*()_+=\;:,"<>?/]'
I guess I am not able to exclude single quote
And why is that, given the fact that I not only gave you a search-term that has your 'hints', but the **EXACT** thing you need to use for a regex to strip out anything but letters and numbers???
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.