PHP Classes

RegEx doesn't match digit-char strings

Recommend this page to a friend!

      Regexp Builder  >  All threads  >  RegEx doesn't match digit-char strings  >  (Un) Subscribe thread alerts  
Subject:RegEx doesn't match digit-char strings
Summary:Unable to convince regexBuilder to match strings like '1st name'
Messages:2
Author:William R Yoder
Date:2010-06-23 19:26:49
Update:2010-06-23 19:28:29
 

 


  1. RegEx doesn't match digit-char strings   Reply   Report abuse  
Picture of William R Yoder William R Yoder - 2010-06-23 19:26:49
I've started using the elegant and sophisticated RegEx module.

However, the simplest task seems not to work. I have a multi-line string, $tcontents, like this:

<tr>
<td>__Salutation.asp</td>
<td>__1st name.asp</td>
<td>__last name.asp</td>
<td>__st. address.asp</td>
<td>__city.asp</td>
<td>__state.asp</td>
<td>__zip.asp</td>
</tr>

I want to replace it with a string like this:
<tr>
<th>Salutation</th>
<th>1st name</th>
<th>last name</th>
<th>st. address</th>
<th>city</th>
<th>state</th>
<th>zip</th>
</tr>

Should be easy, right?

This invocation partially works:

$regexp=new regexpBuilder();
$regexp->match(CHAR)
->frequency(ONE_OR_MORE) //match desired word
->ifItIs(FOLLOWED_BY) //start condition
->match(".asp") //if it's followed by "def"
->closeIf() //close condition
->ifItIs(PRECEEDED_BY) //start condition
->match("__") //if it's preceeded by "__"
->closeIf(); //close condition

$matches=$regexp->execOn($tcontents);

However, it misses any matches that have digits or periods.

I've tried various combinations:

matchOneOfTheseChars
DIGIT_CHAR
NON_GENERAL_SPACE_CHAR
LETTER_CHAR.DIGIT_CHAR."."


They all fail to match.

Any advice? Thanks!

  2. Re: RegEx doesn't match digit-char strings   Reply   Report abuse  
Picture of William R Yoder William R Yoder - 2010-06-23 19:28:29 - In reply to message 1 from William R Yoder
Marco has replied:

I found a simple solution for your problem:

<?php
require_once "regexpBuilder.php";

$tcontents="<tr>
<td>__Salutation.asp</td>
<td>__1st name.asp</td>
<td>__last name.asp</td>
<td>__st. address.asp</td>
<td>__city.asp</td>
<td>__state.asp</td>
<td>__zip.asp</td>
</tr>";


$regexp=new regexpBuilder();
$regexp
->match("__") //Every name comes after a double underscore so match it
->capture() //Start a capturing group for the replacement process
->matchOneOfTheseChars(SPACE_CHAR.LETTER_CHAR.DIGIT_CHAR.".")
->oneOrMoreTimes() //Match every alphabetic and numeric character, whitespaces and dots one or more times
->closeCapture()
->ifItIs(FOLLOWED_BY) //Check those character until the ".asp" string is found
->match(".asp")
->closeIf()
->match(".asp"); //Then match the ".asp" string to replace it

//Try the match
$matches=$regexp->execOn($tcontents);

//Or replace everything with the first capturing group
$replace=$regexp->replaceWith("$1",$tcontents);

/*********
Result:
<tr>
<td>Salutation</td>
<td>1st name</td>
<td>last name</td>
<td>st. address</td>
<td>city</td>
<td>state</td>
<td>zip</td>
</tr>
*********/
?>

---------------------------
Thank you, Marco!

Bill Yoder