473,385 Members | 1,397 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

why does this regular expression fail?

Suppose I have these five lines in a file somewhere:

<p>This entry should belong to:
<select name="formInputs[id_of_neighborhood_to_which_this_belongs]">
<option value="">(No choice made)</option>
<?php getDatabaseTableValuesInOptionTags("neighborhoods" , "id",
"state"); ?>
</select>

Suppose I open this file and read the contents into a string called
$string. Suppose I then give the string to this function:
function matchAllPhpFunctionsInString($subject=false) {
// 11-08-06 - this is being called in importForm

$pattern = "<";
$pattern .= "\?php.*\(.*\); \?";
$pattern .= ">";
$pattern = "/$pattern/";
preg_match_all($pattern, $subject, $matches);
// print_r($matches);

// 09-19-06 - there is no point returning a 2 dimensional array, so we
will make
// it one dimensional.
$arrayOfPhpFunctionNames = $matches[0];
return $arrayOfPhpFunctionNames;
}

This function is suppose to find the PHP command. It works on other
pages, but not the one I've posted above. Why is that? Why would this
regular expression not find this PHP command?

Nov 9 '06 #1
2 1152
lawrence k wrote:
Suppose I have these five lines in a file somewhere:

<p>This entry should belong to:
<select name="formInputs[id_of_neighborhood_to_which_this_belongs]">
<option value="">(No choice made)</option>
<?php getDatabaseTableValuesInOptionTags("neighborhoods" , "id",
"state"); ?>
</select>

Suppose I open this file and read the contents into a string called
$string. Suppose I then give the string to this function:
function matchAllPhpFunctionsInString($subject=false) {
// 11-08-06 - this is being called in importForm

$pattern = "<";
$pattern .= "\?php.*\(.*\); \?";
$pattern .= ">";
$pattern = "/$pattern/";
preg_match_all($pattern, $subject, $matches);
// print_r($matches);

// 09-19-06 - there is no point returning a 2 dimensional array, so we
will make
// it one dimensional.
$arrayOfPhpFunctionNames = $matches[0];
return $arrayOfPhpFunctionNames;
}

This function is suppose to find the PHP command. It works on other
pages, but not the one I've posted above. Why is that? Why would this
regular expression not find this PHP command?

In the pattern you ask for '; ?>', i.e. semi-colon-space-questionmark.

In the code snippet you provided, there is more than one space behind
the semi-colon, ergo: it is correct that the pattern does not match the
code snippet.

If more spaces are allowable, you can of course solve this by changing:
$pattern .= "\?php.*\(.*\); \?";
to:
$pattern .= "\?php.*\(.*\); +\?";
Nov 9 '06 #2

Juliette wrote:
lawrence k wrote:
Suppose I have these five lines in a file somewhere:

<p>This entry should belong to:
<select name="formInputs[id_of_neighborhood_to_which_this_belongs]">
<option value="">(No choice made)</option>
<?php getDatabaseTableValuesInOptionTags("neighborhoods" , "id",
"state"); ?>
</select>

Suppose I open this file and read the contents into a string called
$string. Suppose I then give the string to this function:
function matchAllPhpFunctionsInString($subject=false) {
// 11-08-06 - this is being called in importForm

$pattern = "<";
$pattern .= "\?php.*\(.*\); \?";
$pattern .= ">";
$pattern = "/$pattern/";
preg_match_all($pattern, $subject, $matches);
// print_r($matches);

// 09-19-06 - there is no point returning a 2 dimensional array, so we
will make
// it one dimensional.
$arrayOfPhpFunctionNames = $matches[0];
return $arrayOfPhpFunctionNames;
}


In the pattern you ask for '; ?>', i.e. semi-colon-space-questionmark.

In the code snippet you provided, there is more than one space behind
the semi-colon, ergo: it is correct that the pattern does not match the
code snippet.
Thanks so much! It's funny, I stared at that for a long time, and never
noticed the extra space!

Nov 9 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: hq4ever (at) 012 (dot) net (dot) il | last post by:
function testemail($email) { $validEmailExpr = "^(?)*@(?)*$"; return eregi($validEmailExpr, $email); } $email = "foo@bar.gov.mil"; testmail($email); //return TRUE
9
by: Ron Adam | last post by:
Is it possible to match a string to regular expression pattern instead of the other way around? For example, instead of finding a match within a string, I want to find out, (pass or fail), if...
2
by: Joe | last post by:
Hi, I have been using a regular expression that I don’t uite understand to filter the valid email address. My regular expression is as follows: <asp:RegularExpressionValidator...
5
by: hclugano | last post by:
Hello! I need some help... I have a Text (an SQL-Create-Table-Statement) and have to find the name of the table. There are two ways, the tablename is written: . (new SQL-standard) or...
5
by: tmeister | last post by:
I am in need of a regular expression that tests and fails if there are 14 or more of a character in the test string. There can be up to 13 of these characters in the string and any other...
2
by: Helmut Jarausch | last post by:
Hi, sorry, this seems to be a FAQ but I couldn't find anything I need to check if an object is a compiled regular expression Say import re RX= re.compile('^something') how to test
12
by: =?Utf-8?B?SlA=?= | last post by:
I am a newbie to regular expressions and want to extract a number from the end of a string. The string would have these formats: image/4567 image/45678 image/456789 I would also want to...
4
by: carlos | last post by:
I am working on a regular expression validation for my search page. What I have so far works for most cases, but I would like to fine tune it some. I am new to regular expressions, and I do not...
1
by: sunil | last post by:
Hi, Am writing one C program for one of my module and facing one problem with the regular expression functions provided by the library libgen.h in solaris. In this library we are having two...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.