Questions tagged [regex-alternation]

Anything related to regular expressions alternation operation. An alternation operation in regular expressions syntax is a way of indicating two alternative patterns which can both match the subject string. In many regular expressions flavors (notably those derived from Perl syntax) the alternation is indicated by a vertical bar "|".

Anything related to regular expressions alternation operation. An alternation operation in regular expressions syntax is a way of indicating two alternative patterns which can both match the subject string. In many regular expressions flavors the alternation is indicated by a vertical bar "|".

36 questions
15
votes
2 answers

Why is a character class faster than alternation?

It seems that using a character class is faster than the alternation in an example like: [abc] vs (a|b|c) I have heard about it being recommended and with a simple test using Time::HiRes I verified it (~10 times slower). Also using (?:a|b|c) in case…
Jim
  • 17,102
  • 31
  • 115
  • 227
14
votes
1 answer

Raku regex: Inconsistent longest token matching

Raku's regexes are expected to match longest token. And in fact, this behaviour is seen in this code: raku -e "'AA' ~~ m/A {say 1}|AA {say 2}/" # 2 However, when the text is in a variable, it does not seem to work in the same way: raku -e "my $a =…
Julio
  • 4,491
  • 1
  • 8
  • 33
12
votes
2 answers

Java regex alternation operator "|" behavior seems broken

Trying to write a regex matcher for roman numerals. In sed (which I think is considered 'standard' for regex?), if you have multiple options delimited by the alternation operator, it will match the longest. Namely, "I|II|III|IV" will match "IV" for…
Craig Kovatch
  • 279
  • 1
  • 3
  • 13
12
votes
5 answers

Matching multiple regex patterns with the alternation operator?

I ran into a small problem using Python Regex. Suppose this is the input: (zyx)bc What I'm trying to achieve is obtain whatever is between parentheses as a single match, and any char outside as an individual match. The desired result would be along…
Julian Laval
  • 1,050
  • 4
  • 14
  • 32
10
votes
2 answers

Raku regex: How to know which group was captured at an alternation

With perl (and almost any regex flavour), every group is numbered sequentially. So for example, this code: 'bar' =~ m/(foo)|(bar)/; print $1 // 'x'; # (1-based index) print $2 // 'x'; # (1-based index) prints xbar However, with Raku it behaves…
Julio
  • 4,491
  • 1
  • 8
  • 33
7
votes
2 answers

Is (a|b)* the same as a*|b*?

Is (a|b)* the same as a*|b*? In other words, does (a|b)* accept string which are combinations of as and bs?
Rick Sanchez
  • 3,793
  • 1
  • 22
  • 40
5
votes
1 answer

Why won't a longer token in an alternation be matched?

I am using ruby 2.1, but the same thing can be replicated on rubular site. If this is my string: 儘管中國婦幼衛生監測辦公室制定的 And I do a regex match with this expression: (中國婦幼衛生監測辦公室制定|管中) I am expecting to get the longer token as a match.…
drKreso
  • 972
  • 7
  • 16
5
votes
2 answers

Regex Alternation Order

I set up a complex regex to extract data from a page of text. For some reason the order of the alternation is not what I expect. A simple example would be: ((13th|(Executive |Residential)|((\w+) ){1,3})Floor) Put simply I am trying to either get …
user3649739
  • 1,717
  • 2
  • 14
  • 26
4
votes
1 answer

Regex not matching in Go

I have a regex that is not matching in Go. However in a regex playground it's matching fine: https://regex101.com/r/VNDXcQ/2. It's matching JS comments. Here is the code: comment := "// fallback response. For more information contact support" re :=…
himmip
  • 357
  • 1
  • 4
  • 12
3
votes
3 answers

Any way to use a regular expression with two different pairs of delimiters?

I've added emojis to my Android application and I've been using Regex, in Java, so the codes assigned to them will match the regular expression (which contains a pair of delimiters to be used with), making the characters show up as images. Some…
2
votes
2 answers

How to get the matching word in a regex with alternations?

In python, suppose I want to search the string "123" for occurrences of the pattern "abc|1.*|def|.23" . I would currently do this as follows: import re re.match ("abc|1.*|def|.23", "123") . The above returns a match object from which I can…
2
votes
1 answer

How to search a string for values and convert values

I have an API that accepts a string that needs to be properly formatted before going into the server. The format for going into the server is the following "{Country ABR} {Day/Hour} {State ABR} {Title} {hrs.} ({Month Year}.)" Several Possibilities…
Dylan Godfrey
  • 113
  • 1
  • 10
2
votes
2 answers

Perl uninitialized value when using alternation in regex

I have a for loop with an if statement that looks like this: for (my $i=0; $i < $size; $i++) { if ($array[$i] =~ m/_(B|P|BC|PM)/) { #Remove from @array splice(@array, $i, 1); next; } #Get rid…
N Lindsay
  • 23
  • 2
2
votes
3 answers

why does the first alternation not match?

I have the following regular expression (Python) that I don't understand at the following point. Why doesn't it match the first alternation, too? Regex (spaced for better understanding): (?: \$\{ (?P
Gabriel
  • 7,477
  • 5
  • 47
  • 87
1
vote
2 answers

How to Regex a string

Is it possible to check with regex: the complete string are numbers AND the first character is a 7 or 8 then the complete length of the string must be 11 OR the first character is a 1 then the complete length of the string must be 10 OR the first…
InFlames82
  • 359
  • 2
  • 12
1
2 3