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 1929 Dormilich 8,658
Recognized Expert Moderator Expert
what about a character class? Atli 5,058
Recognized Expert Expert
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
Dormilich 8,658
Recognized Expert Moderator Expert @redbeardmcg
... if you use operators longer than 4 characters... anyway REs are in greedy mode by default.
Atli 5,058
Recognized Expert Expert @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
Markus 6,050
Recognized Expert Expert @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
Recognized Expert Expert
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 docs, but maybe I missed some cleanup or other calls???
Or maybe I messed up passing strings around???
|
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 available or not? If not how could we do this
operation? Any stuffed replies or redirection will be appreciated.
Thanks,
Ganesh
|
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 bc;
string s;
bc = Request.Browser;
....
|
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
purposes.
i have been trying to write the correct pattern for 2 weeks now without
any success...i can get preg_replace() to work, with several simple
patterns but when i use preg_match_all, i either get unintended results
or incomplete matches.
|
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.
example:
.......
| |
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
I've managed to compile and link the C code from "Listing 5", the
regexpSimple() wrapper (see link). The build options are pretty much
|
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 be
accomplished with regular expressions this way, such as validating a
mathematical expression or parsing a language with nested parens,
quoting or expressions.
Another feature I'm missing is once-only subpatterns and possessive
quantifiers...
|
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="' . $prefix . '$2"$3/>', $content);
The problem comes in that it always performs the replace, even when
there's an "http" in the source attribute.
|
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 more information.
None
So I thought I'd try out pcre through ctypes, to recreate pcredemo.c
in python. The c code is at:
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |