IF/then in reg ex. lazarus / 31-LCL ---------------------------------------------------------------------- SteenJorgensen · 2016-10-28 08:36 A small task for large brains I have 2 string that looks like this: String1: ABC-1-2-Q.000 String2: ABC-1-2-Q-OP2.000 This regex will match the first string: (\w*)-(\d)-(\d)-(\w).* $1 = ABC $2 = 1 $3 = 2 $4 = Q But I also want to check if exists one more (-OP) and if it is (true) set $2 to the digit that come after -OP else just $2=1. (\w*)-(\d)-(\d)-(\w)(If -OP then $2=The digit).* $1 = ABC $2 = 2 $3 = 2 $4 = Q How do i do that? ---------------------------------------------------------------------- howardpc · 2016-10-28 11:57 I can't help you with a regex, but a 'brute force' method can also do it (if I've understood your requirement correctly): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program project1; {$Mode objfpc}{$H+}  function FoundOP(const aString: string; out alteredString: string): boolean; var   opPos, hyphenPos: integer; begin   opPos:=Pos('OP', aString);   hyphenPos:=Pos('-', aString);   if (opPos > 0) and (hyphenPos > 0) then begin     alteredString:=Copy(aString, 1, hyphenPos) + aString[opPos+2] +                  Copy(aString, hyphenPos+2, MaxInt);     Exit(True);   end   else begin     alteredString:=aString;     Exit(False);   end; end; var s1: string = 'ABC-1-2-Q.000'; s2: string = 'ABC-1-2-Q-OP2.000'; s: string; begin  WriteLn('FoundOP(s1, s) is ',FoundOP(s1, s),' and s="',s,'"');  WriteLn('FoundOP(s2, s) is ',FoundOP(s2, s),' and s="',s,'"');  //ReadLn;end. ---------------------------------------------------------------------- Martin_fr · 2016-10-28 13:04 Why not simply match (\w*)-(\d)-(\d)-(\w)(?:-OP(\d))?.* The 2 (from -OP2), if present will be in $5. If not present $5 will be empty So you can check in code. The (?:  ) marks a group (but without generating a $n), so the "?" at the end makes the entire group optional. Is there a reason for the .* at the end? It makes no difference. ------------------------ The Problem is you can not change the order of $1 $2 .... so $2 can not match at a position after $4. Therefore you need $5, and then check in code. You can however do this  (ignore/remove spaces, I put them for readability) (?:  (\w*)-(\d)-(\d)-(\w)-OP(\d)  ) | (?:  (\w*)-(\d)-(\d)-(\w)  ) Which will try to match the longer -OP version into $1 to $5. And if no -OP is present, it will match the shorter one into $6 to $9. ------------------------ Actually, if your regex engine supports (?= look ahead then you can do (I dont know if any pascal reg ex engine supports this) Then you could do (\w*)- (?= (?: \d-\d-\w-OP )? (\d) ) \d -(\d)-(\w)  And that gets you $1 to $4 as you want. (tested with the perl regex engine) How does it work? "Look ahead" (?= matches the sub-reg-ex at the current location, but does NOT move forward (matching continues at the current pos afterwards) So the above (\w*)-   matches the first char in $1 (?= (?: \d-\d-\w-OP )? (\d) )  look ahead, if there is an -OP then match the number at the end into $2, otherwise match the number at cur-pos into $2 \d    skip the number at cur-pos, because we matched it in the look ahead -(\d)-(\w)     match $3 and $4 ---------------------------------------------------------------------- sfeinst · 2016-10-28 17:43 I'm not the OP, but very nice Martin