Those would seem to be the three most common use cases for digit matching. In this case, you probably want to be as broad as possible, so ] is the way to go. For example, your program allows the user to input an address, and you want to highlight a possible typo if the input doesn't contain a house number. You have a bit of data that you are not going to use for anything "dangerous", but it would nice to know if it's a number. In this case, you really want, since it's the most restrictive and predictable one. Maybe you want to store it in a numeric field in a database, or use as a parameter to a shell command to run on a server. You have some untrusted user input (maybe from a web form), and you need to make certain it doesn't contain any surprises. \d requires the fewest keystrokes, so it's very commonly used. You can probably tell the number format (by looking at the file) and your current locale, so it's ok to use any of the forms, as long as it gets the job done. You want to extract them for use in your program. Often, when you want to crunch some numbers, the numbers themselves are in an awkwardly formatted text file. Here are some of the more common use cases for matching a digit: The theoretical differences have already been pretty well explained in the other answers, so it remains to explain the practical differences. That means that it accepts most digits but not some nine's (?): $ echo "$str" | sed 's/\")' Should remove only 0123456789 but removes almost all digits. Try grep to discover that it allows most of them: $ echo "$str" | grep -o '\ ' It is generally believed that is only the ASCII digits 0123456789. It match only English Digits: 0123456789. The most basic option for all ASCII digits.Īlways valid, (AFAICT) no known instance where it fails. The \d is supported by few utilities.Īs for, the meaning of range expressions is only defined by POSIX in the C locale in other locales it might be different (might be codepoint order or collation order or something else). The has no possible misinterpretations, ] is available in more utilities and in some cases mean only. So only in C locale all, , \d and ] mean exactly the same. ] is required by POSIX to correspond to the digit character class, which in turn is required by ISO C to be the characters 0 through 9 and nothing else. The \d is not supported (not in POSIX but is in GNU grep -P). ۰۱۲۳۴۵۶۷۸۹ # EXTENDED ARABIC-INDIC/PERSIANĪll of which may be included in ] or \d, and even some cases of. There are many digits in UNICODE, for example: The \d exists in less instances than ] (available in grep -P but not in POSIX). In most programming languages (where it is supported) \d ≡ `]` # (is identical to, it is a short hand for). Yes, it is ] ~ ~ \d (where ~ means approximate).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |