473,406 Members | 2,377 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,406 software developers and data experts.

RegExp help

Note: $code is a single line of code that the previous segment of this
method has located.

I have this...

preg_match('/\bnew wBug\s*\(\s*(.+)\s*\);/i', $code, $arrMatches);
$results = $arrMatches[1];
it will find this...

new wBug ( $myvar ); // <-- this is what $code contains

and it will give me...

$myvar

Which is exactly what I want, for this instance, but if I have this...

new wBug ( $myvar, true ); // <-- this is what $code contains,
this time

I get this...

$myvar, true

I'd like to only get....

$myvar

Actually, I'd like to get each parameter in its own array element of
'$arrMatches'

My RegExp is limited on this one.

Can someone throw me a bone?

Thx

walter

Nov 8 '07 #1
5 1258

<ot*******@gmail.comwrote in message
news:11**********************@t8g2000prg.googlegro ups.com...
Note: $code is a single line of code that the previous segment of this
method has located.

I have this...

preg_match('/\bnew wBug\s*\(\s*(.+)\s*\);/i', $code, $arrMatches);
$results = $arrMatches[1];
it will find this...

new wBug ( $myvar ); // <-- this is what $code contains

and it will give me...

$myvar
wait, wait, wait. so you are opening what apparently is a php script and
parsing it?

try:

/new\s+wBug\s?[^(]*\(\s*([^,)]*)[,)]/i
Nov 8 '07 #2
On Thu, 08 Nov 2007 20:41:50 +0100, ot*******@gmail.com
<ot*******@gmail.comwrote:
Note: $code is a single line of code that the previous segment of this
method has located.

I have this...

preg_match('/\bnew wBug\s*\(\s*(.+)\s*\);/i', $code, $arrMatches);
$results = $arrMatches[1];
it will find this...

new wBug ( $myvar ); // <-- this is what $code contains

and it will give me...

$myvar

Which is exactly what I want, for this instance, but if I have this...

new wBug ( $myvar, true ); // <-- this is what $code contains,
this time

I get this...

$myvar, true

I'd like to only get....

$myvar

Actually, I'd like to get each parameter in its own array element of
'$arrMatches'

My RegExp is limited on this one.
First of all, are you sure regex is the way to go here? It looks awfully
like a parser should be involved...

And while it's perfectly doable to create a regex for it, why not apply
KISS and:

if(preg_match('/\bnew wBug\s*\(\s*(.+)\s*\);/i', $code, $arrMatches)){
$results = $explode(',',arrMatches);
array_walk($results,'trim');
}
It's a LOT easier to maintain then a regex...
--
Rik Wasmus
Nov 8 '07 #3
It's a LOT easier to maintain then a regex...
--
Rik Wasmus
Thanks Rik!

Walter

Nov 9 '07 #4
On Nov 9, 3:41 am, "otrWal...@gmail.com" <otrWal...@gmail.comwrote:
Note: $code is a single line of code that the previous segment of this
method has located.

I have this...

preg_match('/\bnew wBug\s*\(\s*(.+)\s*\);/i', $code, $arrMatches);
$results = $arrMatches[1];

it will find this...

new wBug ( $myvar ); // <-- this is what $code contains

and it will give me...

$myvar

Which is exactly what I want, for this instance, but if I have this...

new wBug ( $myvar, true ); // <-- this is what $code contains,
this time

I get this...

$myvar, true

I'd like to only get....

$myvar

Actually, I'd like to get each parameter in its own array element of
'$arrMatches'

My RegExp is limited on this one.

Can someone throw me a bone?

Thx

walter
how about this, no regexp, just simple sub strings

$string = 'new wBug ( $myvar , true )';
$firstpos = strpos($string,"(");
$lastpos = strrpos($string,")");
$str=substr($string,$firstpos+1,$lastpos-$firstpos-1);
if ( strstr($str,",") )
{
$var=explode(",",$str);
echo $var[0];
}
else
{
echo "$str\n";
}

Nov 10 '07 #5

<mi*******@gmail.comwrote in message
news:11**********************@k35g2000prh.googlegr oups.com...
On Nov 9, 3:41 am, "otrWal...@gmail.com" <otrWal...@gmail.comwrote:
>Note: $code is a single line of code that the previous segment of this
method has located.

I have this...

preg_match('/\bnew wBug\s*\(\s*(.+)\s*\);/i', $code, $arrMatches);
$results = $arrMatches[1];

it will find this...

new wBug ( $myvar ); // <-- this is what $code contains

and it will give me...

$myvar

Which is exactly what I want, for this instance, but if I have this...

new wBug ( $myvar, true ); // <-- this is what $code contains,
this time

I get this...

$myvar, true

I'd like to only get....

$myvar

Actually, I'd like to get each parameter in its own array element of
'$arrMatches'

My RegExp is limited on this one.

Can someone throw me a bone?

Thx

walter

how about this, no regexp, just simple sub strings

$string = 'new wBug ( $myvar , true )';
$firstpos = strpos($string,"(");
$lastpos = strrpos($string,")");
$str=substr($string,$firstpos+1,$lastpos-$firstpos-1);
if ( strstr($str,",") )
{
$var=explode(",",$str);
echo $var[0];
}
else
{
echo "$str\n";
}
usually a great way to go. however when parsing, there's no good way to get
around the literal difference v. abstract differences between 'new wBug (
$myVar' or 'new wbug($myVar' or any other variation. all are the same
when seen by php, but not by strpos - which looks for literal string
occurances. regex can more easily account for any variation of 'new wBug'
and return exactly what the op is looking for.
Nov 12 '07 #6

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

Similar topics

10
by: Anand Pillai | last post by:
To search a word in a group of words, say a paragraph or a web page, would a string search or a regexp search be faster? The string search would of course be, if str.find(substr) != -1:...
19
by: Magnus Lie Hetland | last post by:
I'm working on a project (Atox) where I need to match quite a few regular expressions (several hundred) in reasonably large text files. I've found that this can easily get rather slow. (There are...
5
by: Lukas Holcik | last post by:
Hi everyone! How can I simply search text for regexps (lets say <a href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a dictionary { name : URL}? In a single pass if it could....
4
by: Jon Maz | last post by:
Hi All, I want to strip the accents off characters in a string so that, for example, the (Spanish) word "práctico" comes out as "practico" - but ignoring case, so that "PRÁCTICO" comes out as...
8
by: Dmitry Korolyov | last post by:
ASP.NET app using c# and framework version 1.1.4322.573 on a IIS 6.0 web server. A single-line asp:textbox control and regexp validator attached to it. ^\d+$ expression does match an empty...
6
by: jiing24 | last post by:
I try to use regexp to replace some html tags in DOM, but the result seems some problems, ================================ <Script language="javascript" type="text/javascript"> var config =...
2
by: Uldis Bojars | last post by:
Hi All, I have encountered problems with JS RegExp.exec() and can't find what is the problem. Could you help me? formRequest is a function that extracts some information from XMLHTTPRequest...
2
by: Nathan Sokalski | last post by:
I have the following script that I am using to test some JavaScript RegExp code: function RE() { var testing1=new RegExp("*"); var testing2=new RegExp("{0,}"); var testing3=new RegExp("+");...
4
by: Matt | last post by:
Hello all, I have just discovered (the long way) that using a RegExp object with the 'global' flag set produces inconsistent results when its test() method is executed. I realize that 'global'...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.