2
votes

RegEx pour les URL correspondantes avec un ID de 4 à 6 chiffres

J'essaie de faire correspondre les URL qui commencent par "example.com/" et sont suivies de 4 à 6 chiffres et le caractère suivant n'est pas un chiffre (s'il y a un caractère suivant) .

Par exemple, "example.com/12345" doit correspondre.

"example.com/1234567" ne doit pas correspondre.

"example.com/123456g7" doit correspondre.

J'ai essayé "example.com/(\d{4,6}).*" mais cela correspond quand je le donne "example.com/1234567" qui est incorrect.

Comment résoudre ce problème?


0 commentaires

3 Réponses :


0
votes

Juste recherche anticipée négative pour un chiffre après avoir mis en correspondance 4 à 6 chiffres:

example.com\/\d{4,6}(?!\d).*

https://regex101.com/r/YWmhgY/1/


0 commentaires

1
votes

Une autre façon de le faire.

^ exemple \ .com / (\ d {4,6}) (?: \ D. *)? $


2 commentaires

Que signifie le signe deux-points dans (?: \ D. *) ?


@ user101 - Il fait partie d'un (?: à 3 caractères au début d'une construction de groupe sans capture. Dans ce cas, il est utilisé pour indiquer au moteur que son contenu est \ D. * < / code>, en tant que groupe, sont facultatifs.



1
votes

Cette expression ajoute des limites supplémentaires juste pour passer en toute sécurité les URL souhaitées:

# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r"^(https?:\/\/(www.)?)(example\.com)\/(?:[0-9]{4,6})?([a-z].*)?$"

test_str = ("http://example.com/12345\n"
    "https://example.com/123456g7\n"
    "http://www.example.com/12345\n"
    "https://www.example.com/123456g7\n"
    "http://www.example.com/12345\n"
    "https://www.example.com/123456g7\n"
    "http://www.example.com/123456adfasdfasdf98989898\n"
    "https://www.example.com/123456g7adfadfa0909009\n"
    "http://example.com/1234567\n"
    "https://example.com/1234567")

matches = re.finditer(regex, test_str, re.MULTILINE)

for matchNum, match in enumerate(matches, start=1):
    
    print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
    
    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1
        
        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

Si vous le souhaitez, vous pouvez réduire les limites. Ici, nous pouvons ajouter plusieurs groupes de capture pour être simples à appeler.

$ est la clé qui échoue vos entrées d'URL indésirables.

entrer l'image description here

RegEx

Si ce n'était pas l'expression souhaitée, vous pouvez modifier / changer vos expressions dans regex101.com .

Circuit RegEx

Vous pouvez également visualiser vos expressions dans jex.im : p >

 entrez la description de l'image ici

Démo JavaScript

const regex = /^(https?:\/\/(www.)?)(example\.com)\/(?:[0-9]{4,6})?([a-z].*)?$/gm;
const str = `http://example.com/12345
https://example.com/123456g7
http://www.example.com/12345
https://www.example.com/123456g7
http://www.example.com/12345
https://www.example.com/123456g7
http://www.example.com/123456adfasdfasdf98989898
https://www.example.com/123456g7adfadfa0909009
http://example.com/1234567
https://example.com/1234567`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

Test Python

^(https?:\/\/(www.)?)(example\.com)\/(?:[0-9]{4,6})?([a-z].*)?$


0 commentaires