Capture words using Regular Expressions

2

I have the following text example:

  

MyRelationsPDF001-Cover-Disclaimer-22012017

I want to capture with php regular expressions the texts

  

"Cover", "Disclaimer" and "22012017".

I'm trying to use the preg_match_all() function as follows:

preg_match_all("MeusRelatoriosPDF001-(\w*)-(\w*)-(\w*)",$links,$array);

Where in the parameter $links comes texts separated by the indicated strokes. It is also worth mentioning that the 3 parameters in the variable do not always come. Ex: The variable $link could come only

  

"MyRelationsPDF001-Cover-Disclaimer" or "MyRelationsPDF001-Cover"

The error that is popping up is

  

"Warning: preg_match_all (): Delimiter must not be alphanumeric or backslash".

Can anyone help me how could I capture these texts and put each variable in a $array position?

    
asked by anonymous 02.08.2017 / 17:31

2 answers

4

Every regular expression based on PCRE is necessary to place the most common delimiters are the \ bars but may be other non-alphanumeric characters.

You can simplify your regex and capture in a group only words preceded by a - dash. The capture you want is in the group or it should be accessed by the index 1 ex: $m[1][0] or $m[1][1] .

$link = array('MeusRelatoriosPDF001-Capa-Disclaimer', 'MeusRelatoriosPDF001-Capa', 'MeusRelatoriosPDF001-Capa-Disclaimer-22012017');

foreach ($link as $item){
    preg_match_all("#-(\w+)#", $item, $m);

    echo "<pre>";
    print_r($m);
}   
    
02.08.2017 / 17:59
1

Assuming your links are all in a string separated by \n .

You can use REGEX: ~MeusRelatoriosPDF\d+\-(\w+)(?:\-(\w+))?(?:\-(\w+))?~

$links = 
"MeusRelatoriosPDF001-Capa-Disclaimer-22012017
MeusRelatoriosPDF001-Capa-Disclaimer
MeusRelatoriosPDF001-Capa";

preg_match_all('~MeusRelatoriosPDF\d+\-(\w+)(?:\-(\w+))?(?:\-(\w+))?~', $links, $match);

print_r($match);

Explanation

  • MeusRelatoriosPDF - Literal capture of MeusRelatoriosPDF
  • \d+ - sequence capture of numbers, just left a little generic for other files.
  • \-(\w+) - literal capture of - , and generates the group with the word
  • (?:...)? - Group without optional count, capture if any
02.08.2017 / 19:42