2012-05-26 6 views
0

Je suis en train de faire correspondre le début de ces lignes pour obtenir le numéromatch au début de la ligne d ou d d

1 - blah 
01 - blah 

Je me attends

1 
01 

Je possède ce regex mais ne comprends pas pourquoi la seconde partie ne correspond pas à 01

((^\d)|(^\d\d)) 

merci

Répondre

1

Votre motif ne correspond pas à un positionnement incorrect de ^.

  • ^ sélectionnne le début d'une chaîne sauf si vous utilisez Mode modifier ou autre options.

Essayez cette

(?im)^(\d+)\b 

Explication

<!-- 
(?im)^(\d+)\b 

Match the remainder of the regex with the options: case insensitive (i);^and $ match at line breaks (m) «(?im)» 
Assert position at the beginning of a line (at beginning of the string or after a line break character) «^» 
Match the regular expression below and capture its match into backreference number 1 «(\d+)» 
    Match a single digit 0..9 «\d+» 
     Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» 
Assert position at a word boundary «\b» 
--> 
+0

merci, cela fonctionne pour moi. '^ (\ d +)' –

+1

Si vous voulez spécifiquement les deux premiers chiffres s'il y a un nombre plus long, vous pouvez utiliser '^ (\ d {1,2})' ou de façon équivalente '^ (\ d \ d?)' – tripleee

Questions connexes