What's the difference between \d , [:digit:], and [0-9] in regular expression ?
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.
It seems that \d , [:digit:], and [0-9] are not the same.According to the regular expression reference, \d , [:digit:], and [0-9] have the same meaning, which represent a digit, but why not them work on linux?
Where are you reading the information that all of these should work in bash? Also I think you might want to look up character classes, your digit example, to see their proper use.
It seems Bash doesn't understand \d as other regex engines do. In Bash, \d is a literal "d", not a decimal number. I checked the man pages and couldn't find any reference to \d being interpreted as a decimal number. I could be wrong on that, though...
I'm guessing that you're trying to use \b as a word boundary assertion. Word boundaries work fine in grep with either \b or \< and \>. In PCRE \b also works fine as a word boundary. But I can get it to work in Bash, so I'm beginning to think that either Bash doesn't support \b or I'm doing something wrong. Probably the latter.
Have a look at the manpages for further information: grep(1), regex(7), bash(1) and pcre(3).
You can quote any part of the pattern to force a string match, according to the Bash manpage. But I think you're right MTK358, with single quotes, it seems Bash is matching a literal string.
Yes, thanks! Your solution works fine. It's just that I'm baffled why it doesn't work the other way, with the regex inside the test, that's all. Putting the regex in a variable, as you suggest, does the trick.
Be aware that regex parsing in BASH has changed between releases 3.1 and 3.2. Before 3.2 it was safe to wrap your regex pattern in quotes but this has changed in 3.2. Since then, regex should always be unquoted. You should protect any special characters by escaping it using a backslash. The best way to always be compatible is to put your regex in a variable and expand that variable in [[ without quotes.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.