[SOLVED] Regex search lazarus / 21-General ---------------------------------------------------------------------- pcurtis · 2021-01-08 07:25 Hi All, how do I use regex to search for a string within a string? I am interested in finding if a string contains the following -pxxxx- where xxxx is an integer 0 to 999999? Any help please? Thanks in advance. ---------------------------------------------------------------------- trev · 2021-01-08 10:07 See the Wiki article Regexpr which explains all. ---------------------------------------------------------------------- pcurtis · 2021-01-08 11:30 Not really. It's, like everything else, only a start. What is the regex to find the "-pxxx-" part ---------------------------------------------------------------------- sstvmaster · 2021-01-08 11:37 Something like this: 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 Expression := '[-p]+[\d]+[-]';or 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 Expression := '-p+\d+-'; ---------------------------------------------------------------------- pcurtis · 2021-01-08 11:57 Looking good! I will test. Thanks for the regex voodoo. ---------------------------------------------------------------------- Jurassic Pork · 2021-01-08 12:16 hello, to capture only the digit part of a string you can use a pattern  with a group capture () for digit like this : 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 procedure TForm1.Button2Click(Sender: TObject);var     Re1: TRegExpr;begin      try       re1 := TRegExpr.Create;       re1.Expression := '-p(\d+)-';       if re1.Exec('toto titi  -p239489- tata tutu') then       ShowMessage('Result : ' + re1.Match[1]);       finally       re1.Free;      end;end; Friendly, J.P ---------------------------------------------------------------------- Bi0T1N · 2021-01-08 19:41 Actually the regex from Jurassic Pork would match the string from the first post but also e.g. toto titi  -p239666666666666489- tata tutu. The line re1.Expression := '-p(\d+)-'; should be changed to re1.Expression := '-p(\d{4})-'; for a very precise match of only 4 numbers. In case you want to match 4 to 6 numbers use re1.Expression := '-p(\d{4,6})-';. It would also be better to escape the - as \- to avoid unwanted behaviour: '\-p(\d{4})\-' ---------------------------------------------------------------------- Jurassic Pork · 2021-01-09 06:50 hello, Quote from: Bi0T1N on January 08, 2021, 08:41:00 pmIt would also be better to escape the - as \- to avoid unwanted behaviour: '\-p(\d{4})\-' Escape only (if wanted) those characters outside character classes: .^$*+?()[{\| and these inside character classes (inside brackets []): ^-]\ so you don't need to escape  - in my pattern. Friendly, J.P