473,385 Members | 1,326 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.

pcre returning shortest match

Hi everyone,

I'm having an issue splitting a string with preg_split.... Here is a sample string:

Expand|Select|Wrap|Line Numbers
  1. congestion >= 80
Where >= could also be <=, ==, !=, < or >

What I need to accomplish is splitting this, and put each of the three parts (item, operator, value) into an array.... Here is how I'm doing this:

Expand|Select|Wrap|Line Numbers
  1. $this_expr = preg_split('/(==|>=|<=|<|>|\!=)/U', $rule, -1, PREG_SPLIT_DELIM_CAPTURE);
This works great so long as the operator is one of ==, !=, <, or >.... but where the operator is >= or <= (as in this case) pcre matches ONLY the > or < so that the resultant array looks like this:

Expand|Select|Wrap|Line Numbers
  1. $this_expr[0] = congestion;
  2. $this_expr[1] = >;
  3. $this_expr[2] = =80;
Clearly the operator should be >= and the value should be only 80.... Any ideas of how I could get around this? I did try changing the order (put >= | <= after the < | > in the sub-patter) to no avail... It seems to return only the shortest match.

I could of course write a HACK (if the value starts with = append it to the operator and strip it from the value) but I am so dead set against hacking things together that it hurts me to even consider that as an option.

Thanks!

-Ryan
May 31 '09 #1
8 1916
Dormilich
8,658 Expert Mod 8TB
what about a character class?
Expand|Select|Wrap|Line Numbers
  1. [<>=!]{1,3}
May 31 '09 #2
Atli
5,058 Expert 4TB
Hi.

This doesn't seem to happen on my side.
I tried this code:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. header("content-type: text/plain");
  3.  
  4. $str = "congestion >= 80";
  5. $reg = '/(==|>=|<=|<|>|\!=)/U';
  6.  
  7. $out = preg_split($reg, $str, -1, PREG_SPLIT_DELIM_CAPTURE);
  8.  
  9. print_r($out);
  10. ?>
And I got these results:
Expand|Select|Wrap|Line Numbers
  1. Array
  2. (
  3.     [0] => congestion 
  4.     [1] => >=
  5.     [2] =>  80
  6. )
Isn't that what you would expect?

P.S.
Using PHP 5.2 over here.
May 31 '09 #3
Thank you both for the responses... Dormilich, your example should work however I think you need to change [<>=!]{1,3} to [<>=!]+ to make it more greedy.

Atli, I'm running php 5.1.6 :-/

I did however realize that I am totally over-engineering this.... They are always separated by a space... Using explode(' ', $string) works more than fine!

Sometimes simple is better.

Thanks again!

-Ryan
May 31 '09 #4
Dormilich
8,658 Expert Mod 8TB
@redbeardmcg
... if you use operators longer than 4 characters... anyway REs are in greedy mode by default.
May 31 '09 #5
Atli
5,058 Expert 4TB
@redbeardmcg
How about: '/\s*?([<>=!]{1,3}?)\s*?/U'.
Even strips away the spaces.

Anyways, glad you found a solution :)
May 31 '09 #6
You're absolutely right, I spoke too soon. Thanks again guys!

-Ryan
May 31 '09 #7
Markus
6,050 Expert 4TB
@Atli
Being slightly picky here, I apologise.

Wouldn't that allow for ==>, ==<, etc? Not sure if you are bothered about it, but if you are, you could improve it a little.

Maybe: '/\s*?([<>!]{1}[=]{1,2}?)\s*?/U'

Would the square brackets be needed around the '='? Also, would the above work? I haven't tested it.

Mark (off to read Lemony Snicket's: A Series of Unfortunate Events :)
May 31 '09 #8
Atli
5,058 Expert 4TB
Yea, that's even better. Makes sure you don't get matches on operators like <<< or >>=.

@Markus
No, you don't need to create a character class for a single char.
You can just do: '={1,2}'

@Markus
A very good story. One of my favorites :)
(The movie was good to... Well, how could it not; starring Jim Carrey :P)
Jun 1 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: news | last post by:
Hi all, I am a beginner on 'c' and pcre...and I am on windows 2000 and VC6 with all of the patches, etc. The following program leaks lots of memory and I only make 1 pcre call. I read the...
17
by: sgane2001 | last post by:
Hi, I'm using pcre.c to provide regex support for my application software. I want to know how to do 'AND' operation in this. I didn't got any useful info from the net. Is this operator...
4
by: Earl T | last post by:
When I try to get the netscape version for version 7, I get the HttpBrowserCapabilities class returning the version as 5 and not 7. (see code and output below) CODE HttpBrowserCapabilities...
10
by: greatprovider | last post by:
i'm starting with a string such as "Na**3C**6H**5O**7*2H**20" im attempting to match all **\d+ ...once i can match all the double asterix \d i intend to wrap the \d in "<sub>" tags for display...
16
by: mainland | last post by:
I use prce to match string. I define a pcre struct point, then compile, pass pcre struct point to 'pcre_compile'. When complete, Is it necessary to use 'pcre_free' to free pcre struct point. ...
2
by: Serman D. | last post by:
Hi all, I'm trying to complete the samples from the excellent 2003 developerWorks article "Bringing the Power of Regular Expression Matching to SQL" by Knut Stolze: http://tinyurl.com/3bhrnn...
13
by: Wiseman | last post by:
I'm kind of disappointed with the re regular expressions module. In particular, the lack of support for recursion ( (?R) or (?n) ) is a major drawback to me. There are so many great things that can...
3
by: Jim | last post by:
Hi, I'm trying to prefix the "src" attribute of all "img" elements with a given string, $prefix. Here's what I've got: preg_replace('/\<img(.+?)src="(?<!=http)(.+?)"(.+?)\/>/', '<img $1src="'...
1
by: moreati | last post by:
Recently I discovered the re module doesn't support POSIX character classes: Python 2.5.2 (r252:60911, Apr 21 2008, 11:12:42) on linux2 Type "help", "copyright", "credits" or "license" for...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
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?

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.