0

I am new to job as well as for regular expressions. I am using php.

For the following string i want to extract the report number.

Dear Patient! (patient name) Your Reports(report number) has arrived.

can someone help me in creating a regular expression.

thank you

Solved:

$str ='Dear Patient! (P.JOHN) Your Reports (REPORTNO9) has arrived.';
$str = str_replace('(', '', $str);
$str = str_replace(')', '', $str);
preg_match('/Reports\s*(\w+)/', $str, $match);
echo $match[1]; //=> "REPORTNO9"
6
  • regular-expressions.info you should try creating it yourself first. Commented Sep 20, 2014 at 19:50
  • Please provide a real life example. i.e. what can the patient name and report number look like. Commented Sep 20, 2014 at 19:52
  • @andy: Thank you, I know this is not the right place to ask such question. I don't have enough time for learning. I sure will learn myself. without learning I cant continue this job any further. Commented Sep 20, 2014 at 19:54
  • @UnamataSanatarai patient name contains a-zA-Z. , report number contains A-Z0-9 Commented Sep 20, 2014 at 19:55
  • @UnamataSanatarai P.JOHN,REPORTNO9 Commented Sep 20, 2014 at 19:57

2 Answers 2

1

The Regular Expression

/Dear (\w+)! Your Reports(.*?)(?=has arrived)/

PHP usage

<?php
$subject = 'Dear Patient! Your Reports(report number) has arrived.';
if (preg_match('/Dear (\w+)! Your Reports(.*?)(?=has arrived)/', $subject, $regs)) {
    var_dump($regs);
} 

Result

array(3) {
  [0]=>
  string(42) "Dear Patient! Your Reports(report number) "
  [1]=>
  string(7) "Patient"
  [2]=>
  string(16) "(report number) "
}

Explanation

"
Dear\             # Match the characters “Dear ” literally
(                 # Match the regular expression below and capture its match into backreference number 1
   \w                # Match a single character that is a “word character” (letters, digits, etc.)
      +                 # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
!\ Your\ Reports  # Match the characters “! Your Reports” literally
(                 # Match the regular expression below and capture its match into backreference number 2
   .                 # Match any single character that is not a line break character
      *?                # Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
)
(?=               # Assert that the regex below can be matched, starting at this position (positive lookahead)
   has\ arrived      # Match the characters “has arrived” literally
)
"
Sign up to request clarification or add additional context in comments.

Comments

0

You can use "split()" to extract the specific part of a string like this, so you don't have to use regex :

<?php
    $my_string = ""; // Put there you string
    $array_my_string = array();

    $array_my_string = split('Reports', $my_string);

    $tempResult = array_my_string[1]; // Will contains "(report number) has arrived."

    $array_my_string = split(' has arrived', $tempResult);

    $finalResult = $array_my_result[0]; // Will contains "(report number)"
?>

Comments