2016-04-05 7 views
0

Ich habe sehr einfachen regex Code:Einfacher Regex Invertzucker nicht zurückkehr das volle Wort

(project-(?!old|rejected)) 

Ich habe Liste des String wie:

project-ok 2016/3/4 
project-new 2016/4/5 
project-in-progress 2015/3/8 
project-cancel 2014/2/7 
project-rejected 2011/9/2 
... etc. 

Ich möchte projekt alles außer projekt alt erfassen und Projekt abgelehnt.

Bei dem Versuch, die Zeile übereinstimmen: project-ok 2016/3/4. Ich möchte, dass es das Wort 'Projekt-ok' zurückgibt, aber ich habe den Rückgabewert: 'Projekt-' nur.

Wie wird das gesamte Wort des Projektbezeichners abgeglichen?

+0

['(Projekt- (?! Alt | abgelehnt) \ S +)'] (https://regex101.com/r/sC7iV6/1) – Tushar

+0

@Tushar: Vielen Dank. – andio

Antwort

1

Mal sehen, was Sie versuchen, mit (project-(?!old|rejected))

  • project- Spiele zu tun project-.

  • (?!old|rejected) blickt voraus und prüft, ob old oder rejected vorhanden ist. Wenn JA, stimmen sie nicht überein. Aber nichts zu tun, nachdem es ist nicht vorhanden

So müssen Sie das Etikett entsprechen, bis ein Leerzeichen gesichtet wird. Dies können Sie tun, indem Sie \S+ oder [^\s]+ nach Ihrer vorherigen Bedingung verwenden.

kompletter Regex würde wie folgt aussehen: project-(?!old|rejected)[^\s]+

Regex101 Demo

1

versuchen mit

project-(?!old\s|rejected\s)[-a-z]+

https://regex101.com/r/wX2eY8/3

+0

Vielleicht erklären, warum das besser funktioniert. Das negative Lookahead selbst bestimmt nur, welche Zweige die Regex-Engine nicht verwenden darf, wenn der folgende Text übereinstimmt. – tripleee

0

Sie können dies versuchen:

(?i)(project-(?!old|rejected)\b[-0-9a-z/ ]+) 

Und hier geht die Anatomie der gleichen:

(?i)     # Match the remainder of the regex with the options: case insensitive (i) 
(     # Match the regular expression below and capture its match into backreference number 1 
    project-    # Match the characters “project-” literally 
    (?!     # Assert that it is impossible to match the regex below starting at this position (negative lookahead) 
          # Match either the regular expression below (attempting the next alternative only if this one fails) 
     old     # Match the characters “old” literally 
     |     # Or match regular expression number 2 below (the entire group fails if this one fails to match) 
     rejected    # Match the characters “rejected” literally 
    ) 
    \\b     # Assert position at a word boundary 
    [-0-9a-z/ ]   # Match a single character present in the list below 
          # The character “-” 
          # A character in the range between “0” and “9” 
          # A character in the range between “a” and “z” 
          # One of the characters “/ ” 
     +     # Between one and unlimited times, as many times as possible, giving back as needed (greedy) 
) 

Hoffe das hilft.

Verwandte Themen