Use
[A-Z]?
to make the letter optional. {1}
is redundant. (Of course you could also write [A-Z]{0,1}
which would mean the same, but that’s what the ?
is there for.)
You could improve your regex to
^([0-9]{5})+\s+([A-Z]?)\s+([A-Z])([0-9]{3})([0-9]{3})([A-Z]{3})([A-Z]{3})\s+([A-Z])[0-9]{3}([0-9]{4})([0-9]{2})([0-9]{2})
And, since in most regex dialects, \d
is the same as [0-9]
:
^(\d{5})+\s+([A-Z]?)\s+([A-Z])(\d{3})(\d{3})([A-Z]{3})([A-Z]{3})\s+([A-Z])\d{3}(\d{4})(\d{2})(\d{2})
But: do you really need 11 separate capturing groups? And if so, why don’t you capture the fourth-to-last group of digits?
Related Posts:
- Regex not operator
- Regex not operator
- Regex not operator
- Regex how to match an optional character
- How is the AND/OR operator represented as in Regular Expressions?
- What is lexicographical order?
- working of \n in python [duplicate]
- Extract a substring using PowerShell
- How can I exclude one word with grep?
- Extract substring in Bash
- What is the difference between \r\n, \r, and \n? [duplicate]
- string to string array conversion in java
- What is the difference between \r\n, \r, and \n? [duplicate]
- How to remove punctuation in python?
- How to find all occurrences of a substring?
- How do I properly compare strings in C?
- How to remove .html from URL?
- Split string on whitespace in Python
- What is the difference between a “line feed” and a “carriage return”?
- How to convert an int to string in C?
- Python: TypeError: cannot concatenate ‘str’ and ‘int’ objects
- Ignoring upper case and lower case in Java
- how to replace quotation marks with \”
- Remove all special characters, punctuation and spaces from string
- This can be done without regex:
- How to replace all double quotes to single quotes using jquery?
- Convert character to ASCII numeric value in java
- How to replace ” \ ” with ” \\ ” in java
- What does strcmp() exactly return in C?
- Generate random string/characters in JavaScript
- “[ ]” vs. “[[ ]]” in Bash shell
- What does regular expression \\s*,\\s* do?
- How to trim whitespace from a Bash variable?
- How can I remove punctuation from input text in Java?
- How to get the filename without the extension from a path in Python?
- sprintf like functionality in Python
- C – The %x format specifier
- Lua string.format options
- n-grams in python, four, five, six grams?
- Remove all special characters, punctuation and spaces from string
- Alphabet range in Python
- How can I remove punctuation from input text in Java?
- How can I match a string with a regex in Bash?
- How to trim whitespace from a Bash variable?
- What is the difference between ‘ and ” in JavaScript?
- Center text in fixed-width field with stream manipulators in C++
- C# – Illegal characters in path
- \n or \n in php echo not print [duplicate]
- Mips how to store user input string
- PadLeft function in T-SQL
- bash : Bad Substitution
- How to assign multiple lines string in Powershell Console
- LC-3 STR with R1 as offset
- Lua read beginning of a string
- How can I extract a number from a string in JavaScript?
- Regular Expressions on Punctuation
- How to extract a substring using regex
- How do I print my Java object without getting “SomeType@2f92e0f4”?
- How can I process each letter of text using Javascript?
- PHP to write Tab Characters inside a file?
- Regular expression – starting and ending with a character string
- Haskell: Converting Int to String
- How to use regex in String.contains() method in Java
- Passing string to a function in C – with or without pointers?
- Lua String replace
- Trim a string in C [duplicate]
- How to remove first character from C-string?
- Standard function to replace character or substring in a char array?
- How to count string occurrence in string?
- Difference between char* and char** (in C)
- Using strip() to clean up a string
- Extract filename and extension in Bash
- Meaning of #{ } in Ruby?
- Regex empty string or email
- Taking a new line using printf in java? Is %n correct?
- How to count instances of character in SQL Column
- How to convert/parse from String to char in java?
- How do I make the first letter of a string uppercase in JavaScript?
- Returning an empty string : efficient way in c++
- Best way to strip punctuation from a string
- PHP random string generator
- Finding all possible permutations of a given string in python
- How to check whether a string contains a substring in Ruby
- What does the percentage sign mean in Python
- How can I write a regex which matches non greedy?
- Convert hex string to int in Python
- regular expression “.*[^a-zA-Z0-9_].*”
- How to remove parentheses from string [duplicate]
- Regex Match all characters between two strings
- Javascript Regular Expression Remove Spaces
- Regex pattern inside SQL Replace function?
- Regular expression to stop at first match
- The tilde operator in C
- When to use %r instead of %s in Python? [duplicate]
- Prolog “or” operator, query
- Adding WordPress API Endpoint With Multiple Parameters
- WordPress URL rewrite regex
- How can I globally italicize certain text?
- Redirect after Permalink change – What regex do I use?
- Apostrophes replaced by '