A single regex for two different options

Asked

Viewed 95 times

1

How can I use the regex twice type like this:

let A =  /^A2\d{8}.{26}\d{12}$ , ^A3\d{16}.{26}\d{15}$/;

What would be the way to use it to A2 and A3 without having to use regex twice, like:

let A =  /^A2\d{8}.{26}\d{12}$/;
let A1 = /^A3\d{16}.{26}\d{15}$/;

Here an example:

A220180829Dinheiro                 1000000300767
A310180829Dinheiro                 2000000002600

1 answer

1


Just one detail, one of the regex you used:

let A1 = /^A3\d{16}.{26}\d{15}$/;

Does not match the example text:

A220180829Dinheiro                 1000000300767
A310180829Dinheiro                 2000000002600

'Cause after "A3," she considers it to be 16 digits (\d{16}), but the above text only has 8. Also, at the end of the regex it looks for 15 digits (\d{15}), but the above text only has 13 (so the above regex will not give match in the second line of the text).

So I will consider two options: that the regex is correct and the text is wrong, or vice versa.


Considering the regex and ignoring the example text

If you want a single regex that considers both options, use alternation (considering the regex that are in the question). That is, instead of having these two expressions:

let A =  /^A2\d{8}.{26}\d{12}$/;
let A1 = /^A3\d{16}.{26}\d{15}$/;

I put them together in one:

let a2OUa3 = /^(A2\d{8}.{26}\d{12}|A3\d{16}.{26}\d{15})$/;

The character | means "or". This means that the above regex considers the string may have "A2" (followed by 8 digits, etc) or "A3" (followed by 16 digits, etc).


Considering the example text and ignoring the second regex

Now if you want to consider the text you put in the question (with 8 digits after "A3", and not 16 as it is in the regex of the question), there are other options.

In this case, at first it can be either "A2" or "A3", and the rest is equal, so an alternative is to use a character class:

let a2OUa3 = /^A[23]\d{8}.{26}\d{12}$/;

In the case, [23] means "the digit 2 or the digit 3".


You could also use alternation:

let a2OUa3 = /^(A2|A3)\d{8}.{26}\d{12}$/;

A2|A3 means "A2 or A3" (grouped in parentheses as the | has less precedence).

Of course another option would be:

let a2OUa3 = /^A(2|3)\d{8}.{26}\d{12}$/;

The difference is that the character class can only be used when the options match only one character. The toggle can be used for expressions of any size.

That is to say:

  • (A2|A3) means "A2 or A3"
  • [A2|A3] means "the letter A, or the digit 2, or the character |, or the letter A, or the digit 3" (only one of them - it is worth noting that the letter A is redundant in this case)
  • hkotsubo Actually that one I took as an example just to exemplify even, not that they are equal. But if it’s the case that they’re different the way you would?

  • @Lagaggio If the options are different, use the second solution: (A2|A3) - or more generally, (opcao1|opcao2|opcao3|....). If not, please edit the question

  • I mean if it’s something like that: ^A2\d{8}.{26}\d{12}$ e ^A3\d{10}.{12}\d{12}$

  • @Lagaggio If you want it to be A2 and A3 at the same time, does not give, the regex takes one or the other. In fact, there is no way a text corresponds to these two regex at the same time, or it will be A2, or A3 (or none). Have some examples of texts and the result you need? (ask the question)

  • @Lagaggio I updated the answer, see if it’s what you need...

  • So if you follow your answer if I needed the two to be correct I would add the & sign in the example let a2OUa3 = /^(A2\d{8}.{26}\d{12}&A3\d{16}.{26}\d{15})$/;, or it would be otherwise?

  • @Lagaggio As I said, there’s no way the 2 are correct at the same time. Either the string starts with A2 or A3. If you want a single regex that takes both A2 and A3, the ones I put in already do that (they take both A2 and A3 lines). Or else I couldn’t understand what you need...

  • There is no way she validate both at the same time then? Because there the condition will only validate one. I will have to create a regex for every A1, A2 or A3 I have?

  • @Lagaggio It validates both A2 and A3, see: https://repl.it/repls/LinedFamiliarFossil - I think what confused me was the "at the same time", but anyway, you can use a single regex that validates both A2 and A3 (which is what I put in the answer)

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.