Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Refinement of REGEX in preg_replace stmt

Posted on 2008-06-24
6
234 Views
Last Modified: 2010-04-21
This question is related to my previous question:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_23491772.html

The following preg_replace regex works fine to parse out the comments for arrays like noted in the question above.

// Check and see if line has a comment for language element - assume comment is for last found language variable
if(preg_match('#\'(\w+)\'\s*=>\s*\'([^\']*)\'\s*[,]?\s*(//.*)$#i', trim($line), $match) && !empty($match[1])) {
   $this->_comments[$lastvar][$match[1]] = array('value' => $match[2], 'comment' => $match[3]);
}

What I've noticed now is that some arrays that I need to parse in the file are like the following:

$LANG_GF06 = array (
    1   => 'Statistics',      // Admin Navbar - 1st Tab - Statistics
    2   => 'Settings',        // Admin Navbar - 2nd Tab - Settings
    3   => 'Forums',          // Admin Navbar - 3rd Tab - Forums
    4   => 'Moderator',       // Admin Navbar - 4th Tab - Moderators
    5   => 'Convert',         // Admin Navbar - 5th Tab - Convert (convert stories to forum topics)
    6   => 'Messages',        // Admin Navbar - 6th Tab - Messages
    7   => 'IP Mgmt'          // Admin Navbar - 7th Tab - IP Mgmt (Banned users)
);

Can anyone suggest a tweak to that REGEX so it will handle arrays with a 'text' key as well as numeric keys?
0
Comment
Question by:Bl248
  • 4
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
margajet24 earned 500 total points
ID: 21861579
the "(\w+)" is for alphanumeric characters with one or more repititions.


if that didn't work for you, please try this :

\'?([A-Za-z0-9]+)\'?\s*=>\s*\'([^\']*)\'\s*[,]?\s*(//.*)$
^                         ^

added also "?" to make ' zero or one repitition
0
 

Author Comment

by:Bl248
ID: 21864872
The array that uses numeric keys does not have quotes around the key but it looks like the regex above is still expecting or searching on quote delimiters.
0
 
LVL 10

Expert Comment

by:margajet24
ID: 21865671
\'? - checks whether there is a 1  \' or none..
thus, 0 or 1 repitition..
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:Bl248
ID: 21869404
My new preg_match stmt is:
if(preg_match('#\'\'?([A-Za-z0-9]+)\'?\'\s*=>\s*\'([^\']*)\'\s*[,]?\s*(//.*)$#i', trim($line), $match) && !empty($match[1])) {
    $this->_comments[$lastvar][$match[1]] = array('value' => $match[2], 'comment' => $match[3]);
}

It is still not picking up the integer array records unless I quote them.
Example:
$LANG_GF06 = array (
    1   => 'Statistics',      // Admin Navbar - 1st Tab - Statistics
    '2' => 'Settings',        // Admin Navbar - 2nd Tab - Settings
    3   => 'Forums',          // Admin Navbar - 3rd Tab - Forums
    4   => 'Moderator',       // Admin Navbar - 4th Tab - Moderators
    5   => 'Convert',         // Admin Navbar - 5th Tab - Convert (convert stories to forum topics)
    6   => 'Messages',        // Admin Navbar - 6th Tab - Messages
    7   => 'IP Mgmt'          // Admin Navbar - 7th Tab - IP Mgmt (Banned users)
);

Only record '2' is parsed.
0
 

Author Comment

by:Bl248
ID: 21869431
Sorry - this works which is what you meant:
if(preg_match('#\'?([A-Za-z0-9]+)\'?\s*=>\s*\'([^\']*)\'\s*[,]?\s*(//.*)$#i', trim($line), $match) && !empty($match[1])) {
    $this->_comments[$lastvar][$match[1]] = array('value' => $match[2], 'comment' => $match[3]);
}
0
 

Author Closing Comment

by:Bl248
ID: 31470414
Thanks for the help!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question