A couple of questions before we start:
- what is the regex variant you are using? (they are not all the same)
- is this a "validation" task (i.e. you are only allowing an entry in a field if ALL characters match), a "locate" task (you want to find a sequence of characters within a longer string or a "must include" task (where you are checking that at least 1 (or 2 or...) of the characters?
- is there any structure to the text (i.e. there must be digits THEN whitespace THEN a single backslash and THEN more (or a specified number of) digits?
I am assuming that you are validating the entire text field without structure and so try:
^[\d \\]+$
The way this works is:
^ - matches the beginning of the text; this anchors the match to the start of the text and prevents a match with "invalid" characters before it
[ ] - this is a character set definition (I'll explain the contents shortly)
+ - this is a quantifier that (in this case) tells the regex engine to repeat the item before it (the set definition) as many times as possible but at least once; this forces the match to examine all characters in the text
$ - matches the end of the text; this anchors the match to the end of the text and prevents a match with invalid characters after it
A character set definition is a way of telling the regex engine to match a single character in the text against one of a number of possible characters. In you case those possible characters are the digits a space and the backslash. There are several ways that you can specify the digits including
[0123456789] (the order doesn't matter so this could also be [5823109467]
[0-9] this specifies a range (always from lowest in the ANSI sequence to the highest)
[\d] the '\d' is a special shortcut way of specifying all of the digit characters (actually this also includes all of the "digit" characters defined in the UNICODE list when this is applicable)
The space character is (relatively) easy to specify - just use a literal "space" character. However you do need to be aware that there are options in some regex variants that ignore whitespace in patterns (and even there, there is variability in the interpretation of whitespace within character set definitions). Some prefer to use the '\0x20' form as this makes it very clear that you are using a single "space" character (as opposed to ' ' - is that one or 2 spaces between the quotes???) or even the '\s' option (but that shortcut also includes all of the whitespace characters such as tabs, line-feeds carriage returns etc.).
The backslash character is used to mark the beginning of a special sequence within a pattern. we have already seen that '\d' represents a character set of all digits (as opposed to 'd' which represents a literal lower-case D). Therefore, when we want to represent a literal backslash character we can't just use '\' on its own or it will try to impact on whatever character follows it. Therefore to represent a literal '\' we need to use '\\'.
Therefore, to create a character set definition that represents the characters you want you can write
[\d \\]
as we have above. The order of the items doesn't matter so we could also write '[\\\d ]' etc. or even [ 582\\3109467]. However I prefer to make things as readable as possible and personally I find what I have in the pattern suggestion to be better than the long mess I've just written.
I hope this all makes sense, but please feel free to correct any of my assumptions about the purpose of your pattern or if you don't understand anything.
Susan