Regex allow spaces anywhere meaning. Jun 5, 2019 · This RegEx will allow neither white-space at the beginning nor at the end of your string/word. – Michael. This regex also allowed 1 optional space after digits also. findall. Avinash Raj. Validate your expression with Tests mode. Your regex doesn't check for only one space. In this regular expression, it’s very hard to tell how many spaces are intended to be matched. A regular expression (shortened as regex or regexp ), [1] sometimes referred to as rational expression, [2] [3] is a sequence of characters that specifies a match pattern in text. Check if at least one (ASCII) letter is used. There are four types of lookarounds: Positive Lookahead, Negatve Lookahead, Positive Lookbehind, Negative Lookbehind. value = txt. Colon : is simply colon. The * matches 0 or more of the preceding token. ) . Chat GPT gave the following: May 5, 2014 · 12. – Sam. The (?<=) Positive Look Behind. This is a bit unfortunate, because it is easy to mix up this term May 9, 2014 · no white space at start or end allow white ; space in middle; empty string; But how can I put the quantifiers to limit character count in between 6 - 20? The following should pass "" <-- (empty string) "中文" <-- ( any character) "A B" <-- (allow space in middle) "hi! Hello There" The following should fail The dollar character, also called an anchor, is called the Ending Character. Feb 15, 2018 · 1. When the ^ is inside of a group [^] any characters that come after it are excluded from the match. But, character classes only need certain characters escaped like []. Above expression is only working if I am trying to enter only spaces. * +$. 7) includes some email address validation improvements. Dec 20, 2012 · I wouldn't consider a string with 0 occurrences of numeric values/spaces a string consisting of numeric values/spaces. What you need is to replace "anywhere in the input" with "from the beginning to the end of the input". Jan 26, 2019 · Regex explanation: \( - Literal start parenthesis. Use \A for the beginning of the string, and \z for the end. regular expression for allowing text and spaces but should not begin with spaces (HTML Pages with CSS and JavaScript forum at Coderanch) May 29, 2014 · I'm writing a program that let people enter their infomation (name, age. When multiline is enabled, this can mean that one line matches, but not the complete string. \. key) is false I disable the event. I want to not allow spaces anywhere in the string. Starts with two digits, then a optional group starting with an optional space followed by at least 1 digit. – Jan 26, 2018 · This will match any line that contains '6' even if there are some white spaces or other characters in the line. It cannot be used alone, you need to use it after a pattern. Description. Jun 29, 2012 · For example, Élise Wilson. Mar 8, 2012 · For some languages where space in the regex isn't ignored you can just put the space in directly: [a-zA-Z_0-9 ] will also work. If you’re not using raw strings, then Python will convert the \b to a backspace, and your RE won’t match as you expect it to. 01234 5679 Strings have a match method to match them against a regular expression and a search method to search for one, returning only the starting position of the match. g. 52k 8 81 99. trim() == '' is saying "Is this string, ignoring space, empty?". For instance, the small letter “d” is the metacharacter for a digit, and the capital letter. Oct 22, 2013 · Anywhere except for the beginning and end of the string, and only 1 space allowed after the previous character (the next should be an alphanumeric char inc dash) – Ahmed ilyas Oct 22, 2013 at 0:40 Mar 7, 2023 · In regular expressions, “S” is a metacharacter that represents space. As the name suggests, lookarounds can be look ahead or look behind. In EditPad Pro, turn on the “Dot” or “Dot matches newline” search option. Write your pattern using the special characters and literal characters. so string with one or more white space is restricted. – Feb 12, 2013 · I want to match a string when it contains anything but not 'only spaces'. ”, “*”, “+”, “?”, and more. Matches the end of input. May 19, 2015 · You can optionally have some spaces or underscores up front, then you need one letter or number, and then an arbitrary number of numbers, letters, spaces or underscores after that. A valid name can contain letters in both upper-case and lower-case , may contain - as well. Regular expressions are used in many programming languages, and JavaScript's syntax is inspired by Perl. ]+$. \sa-z]*$/i; const letter = /[a-z]/i; RegExr was created by gskinner. Remarks \s is a common metacharacter for several RegExp engines, and is meant to capture whitespace characters (spaces, newlines and tabs for example). Requirement is "No letter followed by space followed by letter" This needs to work for Spanish characters as well. -] \w is supported in many modern regex engines, but not universal. Jan 2, 2024 · A regular expression (regex for short) allow developers to match strings against a pattern, extract submatch information, or simply test if the string conforms to that pattern. You can also Save & Share with the Community and view patterns I have a username field in my form. However, make sure your careful if you are applying this to . Allowed input examples: group1, group 1, Group1, Group1 &a Apr 7, 2011 · It seems identical to the empty matcher. Instead of using a single regex, you could also opt to use multiple. ^[^\s]. This is true. Feb 7, 2019 · James Coyle. For example: However, if you are looking for an equivalent to trim (), you can match white space before and after a string with: ^[ \t]+|[ \t]+$. Regular expressions can have options, which are written after the closing slash. it matchez a-z as well as A-Z; The regex says: in the beginning, find zero to many alphanums, then a space and finally zero to many alphanums before the string ends; the string you test is input; If you want to make sure that there's at least one alphanum before and after the space, then use 149. [-] matches a hyphen. But with the regex /^[a-zA-Z '. @Junuxx - . Then send an email to that address and make the user click a link within that email to confirm it is a valid address. Jul 6, 2021 · 0. Please suggest! you can do this way also, first trim that email String using trim() function then apply regular expression. In Python’s string literals, \b is the backspace character, ASCII value 8. You can test Regular Expression here: RegExr May 19, 2011 · 2. Only allow the special characters - and '. ), apostrophe ('), and space. Apr 12, 2024 · Here’s how to write regular expressions: Start by understanding the special characters used in regex, such as “. This works. How can I modify this to check if the string is starting with a space, or has two spaces in a row? /^[0-9A-Za-z\s]{1,18}$/. Regular expression for no white space at start or end, but allow white space in middle, but also allow only one char inputs: The closest I have got is : ^([^\s])([\sa-zA-Z0-9_\-]*)([^\s])$ The problem with this is that it will only pass if the input is three or more chars, I need it to accept a single char and pass. You used \w in your first character, which accepted underscores. In the first case, with escaped hyphen ( \- ), regex will only match the hyphen as in example /^[+\-. suggests [0-9A-Za-z ]+. Strip out everything that's not a digit or a dash. Even if I am trying spaces then characters then spaces then expression can't restrict submit. split. A literal space must be backslash escaped in the regex (because spaces are delimiters in Apache config files). ,!\"\'\/$]+/ (just try exec method instead of test and see what it matches) Sep 11, 2017 · I need a regexp that fails in every character is outside [a-zA-Z0-9_& -] Space between characters is allowed, but not empty spaces. Enter your regex: Dec 5, 2011 · That means all letters from all languages in Unicode are already available in \w. const whitelist = /^[-'. Regex lookarounds allow you to match a pattern only if it's followed or preceded by another pattern. as right now it matches any character. The dash -represents a range of characters. this group can be repeated 0 or more times. e. Hello I have a RegEx that allows up to 18 characters with numbers, letters and spaces only. The side bar includes a Cheatsheet, full Reference, and Help. Returns a list containing all matches. May 30, 2024 · In PowerGREP, tick the checkbox labeled “dot matches line breaks” to make the dot match all characters. search. Asking for help, clarification, or responding to other answers. {8,16}$. But there's already an example for that here. Feb 22, 2012 · Not a asp. Oct 1, 2013 · Saying foo. PS: You don't need to escape most of the special regex symbols inside character class as shown in my example. "space or no space" is the same as "zero or one space", or perhaps "zero or more spaces", I'm not sure exactly what you want. Dec 3, 2012 · And I try to edit it to optionally allowed parentheses, dots, dashes, spaces, and plus sign but I'm a beginner so that for now I can't do this to work. Marks the next character as either a special character or a literal. though, just the -. Jan 29, 2017 · But this doesn't allow space at all. By default, the dot will not match a newline character. @Tom, I see you have trouble copying and pasting. Regex allowing a space character in Java. \w{1,12} - Match a word of length 1 to 12 like you wanted. If you use /\S/ it will only match any non-whitespace char, anywhere inside a string. answered Oct 4, 2011 at 12:52. I can't seem to get a match when a space appears anywhere. . Now if after one or more white space if I am entering any other character then its allowing. Jun 10, 2014 · 0. From beginning until the end of the string, match one or more of these characters. – James Coyle. but I cannot find where and how I can add "white spaces anywhere Sep 30, 2017 · Note that although the space is URL encoded (%-encoded) in the request (in order to make valid request), the RewriteRule pattern matches against the %-decoded URL-path, ie. If used as a literal, it can be escaped and put anywhere, or can be put at the beginning or end of the class ([-aa] or [aa-]) and be treated as a literal. I doubt that, this regex matches patterns having zero or more white spaces. Copy the stripped string to a temp var. *)") will either be "fghij" or "abcde\nfghij". org The most common forms of whitespace you will use with regular expressions are the space ( ␣ ), the tab ( \t ), the new line ( \n) and the carriage return ( \r) (useful in Windows environments), and these special characters match each of their respective whitespaces. As others have said, some implementations allow you to control whether the ". ^\d{2}(?: ?\d+)*$ would be a bit more sophisticated. @ArvindKatte I have tired using * & + alternatively both are not working. test(e. Any string that doesn't begin or end with a white-space will be matched. matches any character except line breaks. If you use /^\s|\s*$/ it will match a whitespace at the start or any 0 or more whitespaces at the end. You're also not guaranteed for trim to be defined via a regex, a lot of JS engines have it built-in. Remove any additional blank space if found between parts of names (allow only one space between parts of name - if multiple middle names) Allow diacritics. Then with a single space in-between, it will only match words that have one space between them. No double-spaces - If the user types the space key more than once, do not allow. Jul 2, 2022 · 6. mozilla. 1. edited Jul 2, 2022 at 13:43. For example, it says \< and \> are word boundaries, which is true only (AFAIK) in the Boost regex library. If you really want just alphanumeric characters and input should have two sections with one space between; and invalid characters has to be removed, then: replace all matches of [^\s\dA-Za-z]+ with an empty string, trim leading spaces by replacing ^\s* with an empty string, trim trailing spaces by replacing \s*$ with Apr 25, 2013 · While the regex ends with a * (which means "match 0 or many") it evaluates to true because it doesn't find any of the given characters. The re module offers a set of functions that allows us to search a string for a match: Function. , -, and /: If you have a specific language you're looking to implement this I may be able to help you. I found the text "" starting at index 2 and ending at index 2. Spaces are fine, and can be anywhere as long as there is something else. For the name input I don't want them to left blank and allow only letters and spaces but not start with space. For example: 1) If I don't want to allow any user to enter blank space in the text box, then the regular expression is written as: Regex alphabet=new Regex (@"^ [a-z]*$"); 2) If the user is allowed to enter any blank space in the text box then the regular expression is written as: Apr 28, 2016 · 3. In the following discussion, I'm going to use <space> to represent a single space, since a single space is hard to see in short code snippets. And please note that the user is free what format they are like to use, it simply allow them to input either dashes, space, plus sign, dots, and parentheses. Your regex matches a string that consists entirely of non-whitespace characters and is at least one character in length. – Alan Moore. The dot matches a single character, without caring what that character is. The sequence \\ matches \ and \( matches (. 0123456789. and then replace it with an empty string. Jan 21, 2021 · Which means that if users accidentally have a trailing or leading space in that address (e. Jul 23, 2015 · I mean the same thing in this way. Without the white space allowance, I tried. See full list on developer. In addition, a whitespace special character \s will match any of the specific Feb 27, 2024 · In JavaScript, you can create regular expressions using either a literal notation or the RegExp constructor: Using a Regular Expression Literal: This involves enclosing the pattern between slashes ("/"). '\S' is any char that isn't whitespace. ^[DE]{2}([0-9a-zA-Z]{20})$. The longhand version will work way back to the original Thompson regex engine. PCRE & JavaScript flavors of RegEx are supported. That means it does not accept any symbol besides numeric values and spaces. The regular expression [^A-Z] would match any character NOT A-Z. Remember to use online testers, break down your patterns, and practice regularly to improve your Oct 19, 2012 · I want the user to allow spaces at the beginning and at the end as optional, so I changed the regex as below but it didn't work. A regular expression for a string (or name)which allows lowercase,uppercase letters and spaces. \d : Any digit ( : A literal (. This would match. When you use this special character in a pattern, it does the opposite of the beginning special character; it matches the end part of a string, if that end part matches our pattern. I found the text "" starting at index 1 and ending at index 1. Mar 30, 2011 · The i following the regex means ignore case, i. Their replace method can replace matches of a pattern with a replacement string or function. I found the text "" starting at index 0 and ending at index 0. If you need more characters, just add them inside the class. a literal space. Provide details and share your research! But avoid …. In you scenario this would be 2: Check if only whitelisted characters are used. Jan 1, 2013 · But if you wanted to write an excluded group of characters in your regular expression you can put a ^ in your group. If you would like to remove any white space character anywhere in the string, then you could use the below as your match: [\s\n\r]+. "(\w\s)+$" : this must match a word with a white space at least and must finish with white space. [abc-] matches a, b, c or a hyphen. Yes, the regex is simple, but it's still less clear. Ensure that you have 10 digits left over. May 30, 2024 · Lookahead and lookbehind, collectively called “lookaround”, are zero-length assertions just like the start and end of line, and start and end of word anchors explained earlier in this tutorial. You used + for your following characters, meaning one or more, so a one-character string like '_' wouldn't match. Aug 6, 2012 · I have the following javascript that does not allow the user to input any special characters into a field, but I do want to make an exception and allow for a dash (-): function Validate(txt) { txt. Thus: - matches a hyphen. Use a front end library such as mailcheck to assist users when typing in their address but allow the user to enter what they want. So I want to allow trailing and leading spaces on the above expression. I have used this regex: var regexp = /^\S/; This works for me if there are spaces between the characters. So you just need to add the space and you are done: ^[\w ]{4,11}$. The user should be allowed to leave in white spaces and input should be validated to have a starting DE and then exact 20 characters numbers and letters. many) like this: /[A-Za-z0-9 _. This one works like a charm. For more info look at section POSIX character classes (US-ASCII only) in this document. May 26, 2024 · RegEx syntax reference . The ' [\w ]' is a character class that includes now all characters included in \w and the space. You need. Edit the Expression & Text to see matches. Jan 6, 2015 · Regex for username that allows numbers, letters and spaces (3 answers) Closed 9 years ago . for Example - "ABC X", "ABC XC" are allowed but "C X", "ABC X C" are not. Dec 1, 2017 · ^ asserts that the regular expression must match at the beginning of the subject [] is a character class - any character that matches inside this expression is allowed; A-Z allows a range of uppercase characters; a-z allows a range of lowercase characters. (?:\s+\w{1,12})* - You need this pattern so it can match one or more space followed by a word of length 1 to 12 and whole of this pattern to repeat zero or more times. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation. It doesn't work. I am binding keypress to input field on html and check for the pattern and if regex. +[^\s]$. net or validation expert. Try this: ^. ^[A-Za-z0-9_. What is the appropriate regex? I tried with : ^[a-zA-Z\\s]*$ but it didn't work. This main part of this is: \p{L} which represents \p{L} or \p{Letter} any kind of letter from any language. Something that contains only spaces and underscores will fail the [A-Z0-9] portion. After a few hours of research, I've tried these Sep 2, 2011 · The pattern you're looking for, that matches only those strings with numbers, . Mar 2, 2018 · I got a reference from the link: Javascript validation to allow only Alpha characters, hyphen (-), dot (. But use of \s has to be checked. Mar 23, 2011 · ^ : Start anchor [ : Start of char class. const re = /pattern/; // example const re = /ab+c/; Using the Constructor Function: RegExp constructor. The . ( I need special character only for the middle name) So I tried following regular If you want to check for white space in middle of string you can use these patterns : "(\w\s)+" : this must match a word with a white space at least. value. Oct 24, 2013 · 1. "\n" matches a newline character. Choose a programming language or tool that supports regex, such as Python, Perl, or grep. This regex works for C#, PCRE and Go to name a few. Use \p{Alpha} which is an alphabetic character: [\p{Lower}\p{Upper}] With the above your regular expression will be \p{Alpha}+, which matches one or more of alphabetic characters. ]+$/. \) - Literal closing parenthesis. Alex Howansky. And I have exactly the same requirement. That's how the pattern for most metacharacters works. What to do to allow 0 or more whitespaces anywhere in the string and one or more characters anywhere in the string. If so, use the temp var as your value. \S+ \S+. You can also add more space characters (at the end and beginning) but after the first spacing must be at least one allowed character Good: " some 123 exa_mple m-s-g " Sep 13, 2021 · 1. RegEx Functions. Line 28 corresponds to the committed regex: Dec 29, 2017 · I referred this answer regex for no whitespace at the begining and at the end but allow in the middle. May 17, 2011 · but this will allow 5 spaces in the beginning. For case sensitive regular expression queries, if an index exists for the field, then MongoDB matches the regular expression against the values in the index, which can be faster than a collection scan. It’s better to use only one space and then specify how many spaces are expected, such as: var re = /foo {3}bar/; Jul 23, 2020 · Now, the regex processing is search forward for the pattern P, without move the cursor. In Perl, the mode where the dot also matches line breaks is called “single-line mode”. But elsewhere it says < and > are metacharacters and must be escaped (to \< and \>) to match them literally, which not true in any flavor. The small letter “s” metacharacter stands for space, and the capital letter “S” stands for non-space. It doesn’t work with Java’s own character set! You have to JNI over to ICU’s to get anything that works properly — by which I mean, as a bare minimum, complies with the Level 1 requirements for basic Unicode functionality which are spelled out UTS#18 Unicode Regular Expressions Nov 30, 2012 · The regex is good, but the explanation is a bit misleading. It means nothing, except special cases like, for example, clustering without capturing (also known as a non-capturing group): (?:pattern) Also it can be used in character classes, for example: [[:upper:]] However, in your case colon is just a colon. e If you want to match a regex which repeats say between 1 to 12 times, you can give, regex{1, 12} Similarly, if u want to match a space which repeats just two times and not more or less than that, you can give. This ignores digits, underscores, whitespaces etc. Matches the beginning of input. Spaces should not be allowed at the beginning or end of the field. Jun 29, 2023 · No numbers. (EDIT: I am looking to do this in regex as I ultimately want to combine it with other regex patterns using | ) Here is my test code: Oct 21, 2015 · I need to allow white spaces between words but not allow white spaces between characters. The only exception are newline characters. <text field only accept numeric values and spaces>. Use the appropriate Nov 1, 2010 · 236. Sep 13, 2010 · 5. You can check the commit here. com. Also give me some links where i can find loads of already built regular expressions. Test Case: User might enter: Jun 13, 2012 · Here is a regex to match any number of spaces, periods, or letters: Examples: You need to escape the period, i. Jan 26, 2016 at 16:34. Feb 15, 2014 · This result can be translated to : "one or more digits ( \d+) followed by zero or more blank spaces ( \s* ), one or more times ( ()+ ), anywhere in the input ". Improving on the Bart Kiers solution, here's the regex: May 14, 2019 · 93. ) : A literal ) : A space - : A hyphen. 174k 30 240 283. No need to escape it as it is non-special inside char class. javascript. Leading spaces ^\s*: This will match any (*) whitespace (\s) at the beginning (^) of the text. The regular expression [A-Z] would match any character A-Z. My issue is not allowing double spaces anywhere in the string, and also forcing spaces right before MO or GO characters. Feb 13, 2016 · The only allowed characters are A through Z, 0 through 9 or spaces. Because \s not only allows space but also other whitespaces which phone number cannot have. match(/^\s*$/) is asking "Does the string foo match the state machine defined by the regex?". 01 23 45 67 89. Spaces in regexes are only ignored, if the option x is used and its always possible to add a space to a character class, even with x enabled. May 2, 2012 · This means your regular expression doesn't allow any blank spaces. \s denotes white-spaces and so [^\s] denotes NOT white-space. I want to improve this regex to prevent the following: No leading/training spaces - If the user types one or more spaces before or after the entry, do not allow. `. True, and depending on the regex flavor and settings, \w may also match Unicode letters, which may or may not be desirable. i. Do not use regex for email validation. Thus, it does more than just "check if there is at least one non-whitespace character". I need to check an input field for a German IBAN. Explanation: ^ denotes the beginning of the string. 3k 1 41 49. Jan 19, 2014 · "regex": /^[ +-]?\d+ ?$/ Here [ +-]? is a character class with space as 1st character that allows optional space before 1 or more digits. For example: n matches the character n. In the actual regular expression, you must use an actual space character. May 7, 2023 · Regex can be a powerful tool for text manipulation, but it can also be overwhelming and confusing. Line-based regular expression use is usually for command line things like egrep. Note: Spaces may be spaces or tabs. Strip out the dashes. " will match the newline, giving you the choice. Matches the preceding character zero or more times. Enter your regex: Enter input string to search: xx. You can escape the hyphen inside a character class, but you don’t need to. replace(/[^a-zA-Z 0-9\n\r]+/g, ''); } How to modify it to add the dash to the allowed list? Jul 16, 2012 · Problems in your original regex: | doesn't mean alternation in a character class, it means a pipe character literally. 10. Special characters used in your regex: In character class [-+_~. Jun 7, 2010 · If numbers are ok, too, you can shorten this to. is matching "Foo æ ø å" in my test. Value allowed: 0123456789; 012-345-6789 Mar 14, 2017 · You can check for two consecutive spaces by using the repetition regex. Jan 6, 2022 · Here's what I've done so far ^[^ ][a-zA-Z0-9 ,()]*[^;'][^ ]$, which prevents spaces at the beginning and at the end, and also the ";" character. Thanks! Nov 22, 2010 · Try to use this plugin for masking inputyou can also check out the demo and use this plugin if this is what you may want Masked Input Plugin Oct 2, 2008 · Depending on the specifics of the regex implementation, the $1 value (obtained from the "(. It doesn't work for JavaScript on Chrome from what RegexBuddy says. I am using this regex in both native javascript (not jquery) and PHP. See the regex demo. If my regex is right, you should be able to use. function validateName(name) {. Roll over matches or the expression for details. [\w. answered Jun 10, 2014 at 4:15. \s{2} Remember that this is a general way of checking the Apr 8, 2011 · That’s the problem with the standard Java regex library. That means that empty string does not violate the condition. \d\w]: Lookarounds in Regex. does not have a special meaning inside of a character class, so escaping it is not necessary in this regex. Dec 26, 2011 · Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. I am using [a-zA-Z0-9_\s-] because I just want to allow English characters. Alternatively, we could say : "at least one digit, anywhere in the input ". Edit: Note that ^ and $ match the beginning and the end of a line. The Addedbytes cheat sheet is grossly oversimplified, and has some glaring errors. The full regex itself: [^\w\d\s:\p{L}] One of the more error-prone things you can do with a regular expression is to use more than one space, such as: var re = /foo bar/; 1. Will this work? ([0-9A-Za-z]+)([ ]*) You can add the RegEx character \s to your regular expression, allowing spaces, whitespace and line breaks: ^[a-zA-Z\s]*$ Or, like the other posters said, use a space, to only allow spaces: ^[a-zA-Z ]*$ Trailing spaces \s*$: This will match any (*) whitespace (\s) at the end ($) of the text. Also I cannot use trim or replace. Returns a Match object if there is a match anywhere in the string. 4 days ago · First, this is the worst collision between Python’s string literals and regular expression sequences. Oct 4, 2011 · Don't bother with regex. Only if it’s in a character class and between two other characters does it take a special meaning. If the pattern fail is OK (negative!) In the case, we have (?!$) , where P is $ , the anchor that indicates the end of the text. Use of character class is right. For the details, please visit The Dot Matches (Almost) Any Character. And 6 matches a "6" Character. Test function will evaluate to false only if you change that "*" with a "+" (which means 1. when copy/pasting), the expression validates to false. Dec 20, 2012 at 13:35. \h can also be used, which is horizontal whitespace, or just a plain space ( [\d ] ). The \A (start of string) and \Z (end of string) anchors are not supported by JS RegExp. Your regex would be like this to find the lines which has leading and trailing spaces, ^ +. I am trying to create a function to validate name in my application. -]+$/ following strings are also accepted: tavish. You don't need to escape the / and . All symbols and letters are included in '\S', and we have '+' for any 1 or more of those. 5. The hyphen is usually a normal character in regular expressions. Saying foo. The difference is that lookaround actually matches characters, but then gives up the match, returning only the result: match or no match. matches a period rather than a range of characters \s matches whitespace (spaces and tabs) Jun 29, 2016 · The newest release of angularJs (1. With the ultimate regex cheat sheet, you now have a comprehensive guide to regex syntax, quantifiers, character classes, and advanced techniques. – user2705585. Apr 9, 2014 · Nice idea use the negative regex, but using the character group for a single one is unneeded, just use: ^(?!! )[A-Za-z ]*(?<! )$ @Tom, it is fairly unfair that you edit your answer and answer to my comment stating it does not make sense. Generally with hyphen ( -) character in regex, its important to note the difference between escaping ( \-) and not escaping ( -) the hyphen because hyphen apart from being a character themselves are parsed to specify range in regex. To list a literal hyphen in char class place it at the beginning or at the end without escaping it or escape it and place it anywhere. Further optimization can occur if the regular expression is a "prefix expression", which means that all potential matches start with the same Bart Kiers solution is good but it misses rejecting strings having spaces and accepting strings having underscore (_) as a symbol. qf yy wt pr vp gh nf gy ty qg