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?

Answer from Tim Pietzcker on Stack Overflow
🌐
RegexOne
regexone.com › lesson › optional_characters
RegexOne - Learn Regular Expressions - Lesson 8: Characters optional
Another quantifier that is really common when matching and extracting text is the ? (question mark) metacharacter which denotes optionality. This metacharacter allows you to match either zero or one of the preceding character or group.
🌐
freeCodeCamp
freecodecamp.org › news › how-do-i-make-regex-optional-specify-optional-pattern-in-regular-expressions
How Do I Make RegEx Optional? Specify Optional Pattern in Regular Expressions
March 9, 2023 - By putting the question mark symbol in front of the letter “u”, it means the "u" is optional. So, "color" and "colour" get matched, "behavior" and "behaviour" also get matched.
🌐
Reddit
reddit.com › r/regex › match optional strings in a pattern
r/regex on Reddit: match optional strings in a pattern
July 14, 2022 -

hey, I'm using python and I'd like to match shop no. strings like:

SHOP NO. 3 AND 4 AT PLOT NO. 24                             - string 1
FIRST FLOOR OF SHOP NO. 7 ATLANTIC                          - string 2
SHOP NO-33                                                  - string 3

this is what i expect:

3 AND 4
7
33

i came up with this regex: shop no\W*([\w\-,]*)\b (and|to)? (\d*)\b it matches the second and third stirng but not the first string.

Essentially I want to match the third group if the second group (and or to) exists. It will be even better if i can do this with only one group.

Any help is appreciated. Thanks

🌐
Reddit
reddit.com › r/learnpython › regex optional word not working for simple case
r/learnpython on Reddit: Regex optional word not working for simple case
January 4, 2017 -

I have strings that could be something like this, where "very" is an optional word that may or may not be present:

string1 = "It is very cold outside"      
string2 = "It is cold outside"

I've tried the following regex with the "?:" optional group:

re.findall("It is (?:very) cold outside", string2)

But it doesn't find anyting for string2. I thought it might have something to do with the spaces but "Itiscoldoutside" still doesn't match if I adjust the pattern.

What's the issue here?

🌐
JMP User Community
community.jmp.com › t5 › Discussions › Regex-finding-optional-characters › td-p › 334933
Solved: Regex finding optional characters - JMP User Community
June 9, 2023 - The beginning ^ and ending $ force the regex to match from start to finish. The [^:] matches characters that are not : and [^;] matches characters that are not ; . The lone : gets past the required : and the ;? is an optional ; .
🌐
Medium
rclarke-m.medium.com › regex-series-part-3-special-characters-8b1520619a93
RegEx Series: (Part 3) Special Characters | by R. | Medium
October 16, 2020 - Since the ‘r’ precedes our optional character, our regular expression is looking to match a pattern that at the very least has the string “Teste”. What this is saying is that the ‘r’ is optional, and because it is, it matches the ‘Teste”. As the ‘r’ is present within “Tester”, it also matches the string “Tester”. In this example, the ‘r’ can occur zero or more times.
Find elsewhere
🌐
University of Washington
webinsight.cs.washington.edu › nfbslam › regex.php
Regular Expressions
Regular expressions can also group parts of the expression, which is useful when used with optionally matching pieces of the regular expression. The following regular expression will match "George Bush", "George W. Bush", and "George W Bush", but not "GeorgeBush." if (User.RegexMatch("George (W(.)?
🌐
UiPath Community
forum.uipath.com › help › activities
How to make [ ] as optional in regex - Activities - UiPath Community Forum
September 22, 2021 - Hi, I’m new to UiPath and very new to regex. Need to retrieve values of OnError, Name and Path. Sometimes the values of Name and Path will be in ’ ’ and sometimes in-between ‘[ ]’. Attaching the Screenshot and text f…
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Regular_expressions
Regular expressions - JavaScript | MDN
When you want to know whether a pattern is found in a string, use the test() or search() methods; for more information (but slower execution) use the exec() or match() methods. If you use exec() or match() and if the match succeeds, these methods return an array and update properties of the associated regular expression object and also of the predefined regular expression object, RegExp.
🌐
RegExr
regexr.com › 3dvqn
RegExr: Learn, Build, & Test RegEx
Supports JavaScript & PHP/PCRE RegEx. Results update in real-time as you type. Roll over a match or expression for details.
🌐
Bomberbot
bomberbot.com › regex › how-do-i-make-regex-optional-specify-optional-pattern-in-regular-expressions
How Do I Make RegEx Optional? Specify Optional Pattern in Regular Expressions - Bomberbot
You could match both with: ... However, this doesn‘t make it clear that ‘a‘ is preferred and ‘e‘ is just an acceptable alternative. We can improve this by using the ? to make the ‘e‘ optional: ... This more clearly expresses that we‘re looking for "gray" with an optional "e" character. The ? makes the regex engine match the ‘e‘ zero or one time.
🌐
Splunk Community
community.splunk.com › t5 › Splunk-Search › How-do-you-make-a-regex-that-skips-an-optional-word › m-p › 436799
How do you make a regex that skips an optional wor... - Splunk Community
September 29, 2020 - The '?' after the '(administratively )' capture group, basically tells the regex that the previous group/character is optional.
🌐
Edwinwenink
edwinwenink.xyz › posts › 66-regex_with_starting_and_ending_optional_groups
Regular expressions with optional starting or ending groups
But since we know where to begin matching and we’d like to also capture “verlenging”, we can just anchor the start with a literal match: (?P<verlenging>verlengt|verlenging).{0,50}(?P<TBS1>TBS|terbeschikkingstelling|ter beschikking (?:wordt |is )?(?:stelling|gesteld)) Combining everything we get a quite lengthy regex with two alternatives. Either we require something like “verlenging” in front of the regex, or something like “veroordeling” or “voorwaarden” after. The ending group is now no longer optional:
🌐
Ask LibreOffice
ask.libreoffice.org › english
How to find three optional patterns in one regex (abc–abc, abc –abc, abc– abc), but not a fourth one (abc – abc) - English - Ask LibreOffice
July 4, 2023 - Hi, I want to find word–word word –word word– word but not word – word Presently I am using ([a-z]{1,}|[\u00F0-\u02AF]{1,})–([a-z]{1,}|[\u00F0-\u02AF]{1,}) which may be simplified in some way. I replace the result with: $1 ⁠–⁠ $2 In order to have 4. Now, would there be a way to create one regex that covers also 2. and 3. (but not 4.) and also allows replacements in order to obtain 4.?