Hi everyone,
I'm having an issue splitting a string with preg_split.... Here is a sample string:
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: - $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: - $this_expr[0] = congestion;
-
$this_expr[1] = >;
-
$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
8 1916
what about a character class? Atli 5,058
Expert 4TB
Hi.
This doesn't seem to happen on my side.
I tried this code: - <?php
-
header("content-type: text/plain");
-
-
$str = "congestion >= 80";
-
$reg = '/(==|>=|<=|<|>|\!=)/U';
-
-
$out = preg_split($reg, $str, -1, PREG_SPLIT_DELIM_CAPTURE);
-
-
print_r($out);
-
?>
And I got these results: - Array
-
(
-
[0] => congestion
-
[1] => >=
-
[2] => 80
-
)
Isn't that what you would expect?
P.S.
Using PHP 5.2 over here.
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
@redbeardmcg
... if you use operators longer than 4 characters... anyway REs are in greedy mode by default.
Atli 5,058
Expert 4TB @redbeardmcg
How about : '/\s*?([<>=!]{1,3}?)\s*?/U'.
Even strips away the spaces.
Anyways, glad you found a solution :)
You're absolutely right, I spoke too soon. Thanks again guys!
-Ryan
@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 :)
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) Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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.
...
|
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...
|
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...
|
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="'...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |