Jake
Jake

Reputation: 143

PHP RegEx pattern matching - beginner questions to get me started

This is a homework assignment and my first experience using RegEx. I am starting to grasp the syntax and symbols used and can do some simple pattern matching/manipulation, but can't quite foresee how to achieve some of the goals of this assignment.

I have been given a text file that is formatted like this:

    Steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA 83755:11/12/56:20300
    Betty Boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
    Igor Chevsky:385-375-8395:3567 Populus Place, Caldwell, NJ 23875:6/18/68:23400
    Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700

There are about 50 lines of names and corresponding info, each entry is on a new line and each 'field' is separated by a colon. Mostly I need to find specific things from the file and print them on a webpage but I don't quite understand.

Here is one problem I solved:

$myFile = "datebook.txt";
$data = file($myFile);//I have used this to place all data in an array, but it may be necessary to place the data into a string?

//1)    Print all lines containing the pattern Street (case insensitive).
$pattern = "/street/i";
$linesFound = preg_grep($pattern, $data);
echo "<pre>", print_r($linesFound, true), "</pre>";

Here are some I have not and specific questions regarding them:

2) Print the first and last names in which the first name starts with a letter ‘B’.

How do I only search for first names and not last names, city names, etc?

How do I print the full name and only the full name?

5) Print Lori Gortz’s name and address.

I understand how to find the pattern 'Lori Gortz' but how do I return her address as well?

11) Print lines that end in exactly five digits.

12) Print the file with the first and last names reversed.

14) Give everyone a $250.00 raise.

Don't know how to do any of these. I assume the last number for each entry is their salary.

Any help is appreciated. Please respond with an explanation of the code as well, thank you.

Upvotes: 1

Views: 633

Answers (2)

Shomz
Shomz

Reputation: 37701

Check the RegEx quick reference, I think you'll figure out most of your tasks there. For example, Lori's address would be a string after the number after the second colon and before the second coma (in her line, of course).

The best way to do all the task would be to go over each line and make an array with all the elements. That way you could easy replace names, increase salaries, check if it ends with 5 digits, etc.

You can also try this online tester. Good luck.

Edit: Little help for a start:

^[A-z ]*   this gets full names
^[A-z]*    this gets first names
etc...

Edit2: See what this code does:

$line = "Betty Boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500";
$regex = "/\s|:/";
$result = preg_split($regex, $line);

:)

Upvotes: 1

Mike Christensen
Mike Christensen

Reputation: 91628

I don't want to do all of them, but here's some hints.. For question 2:

^[A-Z]* B.*$
  • ^ basically means a new line.
  • [A-Z]* means any number of characters from A-Z
  • Next we match a space
  • Next we match a B
  • The .* means any number of other characters.
  • Lastly, we match with an end of line using $

This can definitely be improved and made more flexible, but I'll let you do that..

Upvotes: 1

Related Questions