Help with Regex

Asked

Viewed 82 times

6

Before asking I looked for several references, but still do not understand much and I managed to get only to a certain point.

My goal is to validate that a string specific has 10 characters, the first two letters being uppercase, and the other 8 characters being numbers.

The first two letters must be obligatorily AD, AG or EX. May not be AE or EG for example.

To that end I have the following Regex /^[A(D|G)|EX]{2}[0-9]{8}$/gm. But it does not comply with the second rule. It allows the letters AGDEX to be mixed, and not in the specific order desired.

I’m using the Regexr to validate my regex with the following values:

EX09551115
AD09551115
AG09551115

EA09551115
EG09551115
AE09551115
AX09551115
DG09551115
GD09551115
XE09551115
GA09551115
DA09551115
XD09551115
XG09551115
GX09551115
DX09551115

Only bold values shall be valid values.

What I wish is to know how I do to satisfy the second condition. No need to leave a regex ready, just show me the way will already be of great help.

References will also be welcome.

3 answers

9


I believe this regex ^(AD|AG|EX)[0-9]{8}$ or ^(AD|AG|EX)\d{8}$ solves the problem.

At the beginning of the line is expected (a group option) AD or AG or EX followed by numbers that must repeat exactly 8 times.

In a list metacharacters lose their functions [A(D|G)|EX]{2} then the group and the OU(pipe) and you expect it to happen exactly twice.

  • I had tried it, but for a lack of attention I did not remove the {2} that was what ended the condition. Thank you for the reply

  • @Richarddias, the problem wasn’t just the {2} but the list items, take the regex orginal of your question and see if in the entries ||09551115, ((09551115 or ))09551115 match the pattern specified in regex ;)

  • Yes, they hit, that loco. But what I meant, is that I had tried this idea of groups (AD|AG|EX), but had not removed the {2}

  • Some point of the answer is wrong or can be improved?

  • If you have any legal references for those who are starting to understand better, it would be nice, but overall cleaned up my problem.

  • @Richarddias, I can add something later, in the tags there is usually a wiki with information and some materials, click on the tag name, then in the info tab. From regex the address is http://answall.com/tags/regex/info Anything calls there

Show 1 more comment

2

Whoa, buddy, try that regex: https://regex101.com/r/tX6fT8/1

His regex /^[A(D|G)|EX]{2}[0-9]{8}$/gm didn’t work because you didn’t group the letters AD, AG and EX leaving A free along with D, G and EX within the group.

A second option would be this https://regex101.com/r/tX6fT8/2, you can also use the $ and the ^ to define lines if you prefer.

1

It works:

/^(AG|AD|EX)(\d{8})$/gm

This is where the groups in regular expression make sense:
First group is a combination of uppercase letters like AG or AD or EX;
Second group is a sequence of digits having size 8.

Browser other questions tagged

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