473,811 Members | 2,631 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Combining 2 preg matches.

Hi group,

I have a function which validates a string using preg match.
A part looks like

if( !preg_match( '/^([a-z0-9]+(([a-z0-9_-]*)?[a-z0-9])?)$/', $string )
||
preg_match( '/(--|__)+/' ,$string) ) {

i wonder how i could combine those two into one ...
I tried a few different options of putting the second match into the
first one,
using things like [^__]+ etc, but nothing worked for me.
it should prevent double (or more) dashes or underscores behind each
other.
hello-there = ok
hello--there != ok

Any help would be great.

Frizzle.

Jul 15 '06
14 2025
Rik
frizzle wrote:
>>$regex ='/^(?:[a-z]|[a-z][_\-][a-z])+$/';

...will do just fine.

equally so:
$regex ='/^(?:[a-z]+(?:[_\-][a-z]+))+$/';

Lookahead & -behind are unneccessary in this case, and this keep it
simple.
Good point. It doesn't make sense to use assertions when you'll
capture
the matches anyway.
Somehow, i believe Rik's solution, gave me problems ...

'/^(?:[a-z0-9]|[a-z0-9][_\-][a-z0-9])+$/'; gave problems.
'/^(?:[a-z0-9]|(?<=[a-z0-9])[-_](?=[a-z0-9]))+$/' didn't.

An example string that gave problems is:
really_a_made_u p_string

Ah, forgot that in [a-z0-9][_\-][a-z0-9] the character on the right is
already matched, so it won't work as a start for the second _ in _a_....

This one should still be working though:
$regex ='/^(?:[a-z0-9]+(?:[_\-][a-z0-9]+)*)$/';

Grtz,
--
Rik Wasmus
Jul 17 '06 #11

Rik wrote:
frizzle wrote:
>$regex ='/^(?:[a-z]|[a-z][_\-][a-z])+$/';

...will do just fine.

equally so:
$regex ='/^(?:[a-z]+(?:[_\-][a-z]+))+$/';

Lookahead & -behind are unneccessary in this case, and this keep it
simple.
Good point. It doesn't make sense to use assertions when you'll
capture
the matches anyway.
Somehow, i believe Rik's solution, gave me problems ...

'/^(?:[a-z0-9]|[a-z0-9][_\-][a-z0-9])+$/'; gave problems.
'/^(?:[a-z0-9]|(?<=[a-z0-9])[-_](?=[a-z0-9]))+$/' didn't.

An example string that gave problems is:
really_a_made_u p_string


Ah, forgot that in [a-z0-9][_\-][a-z0-9] the character on the right is
already matched, so it won't work as a start for the second _ in _a_....

This one should still be working though:
$regex ='/^(?:[a-z0-9]+(?:[_\-][a-z0-9]+)*)$/';

Grtz,
--
Rik Wasmus
ok, dankjewel / thanks a lot.

Frizzle.

Jul 17 '06 #12
Rik wrote:
Ah, forgot that in [a-z0-9][_\-][a-z0-9] the character on the right is
already matched, so it won't work as a start for the second _ in _a_....
You know, I thought that was the problem initially, but then remembered
that the regular expression engine does backtracking in order to
maximise any match. When it encounters the underscore after assigning
the letter to the first subpattern, it's supposed to abandon the
previous match, backtrack to the letter, and go down the second branch.

Jul 17 '06 #13
Rik
Chung Leong wrote:
Rik wrote:
>Ah, forgot that in [a-z0-9][_\-][a-z0-9] the character on the right
is already matched, so it won't work as a start for the second _ in
_a_....

You know, I thought that was the problem initially, but then
remembered that the regular expression engine does backtracking in
order to
maximise any match. When it encounters the underscore after assigning
the letter to the first subpattern, it's supposed to abandon the
previous match, backtrack to the letter, and go down the second
branch.
Yes and no. It does exactly what you say, but it is simply not valid:

With the pattern:
'/^(?:[a-z0-9]|[a-z0-9][_\-][a-z0-9])+$/';
one states the entire string can be build by either [a-z0-9](1)OR
[a-z0-9][_\-][a-z0-9](2), think of them as blocks.

Let's examine it (not entirely how it works, but this instance close
enough):
(fixed width font is handy now:)
positions: 123456789012345 678901234567890
string: really_a_made_u p_string
match1: 111111_error, let's try the other option.
match2: 111112--_error, no other matches possible.

There is no possibility for a match with either (1) or (2) at the second _,
and no other options to match instead at the beginning of the string.

Grtz,
--
Rik Wasmus
Jul 17 '06 #14
Rik wrote:
Yes and no. It does exactly what you say, but it is simply not valid:

With the pattern:
'/^(?:[a-z0-9]|[a-z0-9][_\-][a-z0-9])+$/';
one states the entire string can be build by either [a-z0-9](1)OR
[a-z0-9][_\-][a-z0-9](2), think of them as blocks.

Let's examine it (not entirely how it works, but this instance close
enough):
(fixed width font is handy now:)
positions: 123456789012345 678901234567890
string: really_a_made_u p_string
match1: 111111_error, let's try the other option.
match2: 111112--_error, no other matches possible.

There is no possibility for a match with either (1) or (2) at the second _,
and no other options to match instead at the beginning of the string.
Ah! I missed the single letter case.

Jul 17 '06 #15

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

Similar topics

5
8264
by: sinister | last post by:
The examples in the online manual all seem to use double quotes, e.g. at http://us3.php.net/preg_replace Why? (The behavior is different with single quotes, and presumably simpler to understand.)
2
1747
by: chris | last post by:
Hi, I would like to take two documents and combine them. I can do this but I'm having a little problem with namespaces again. The input documents namespace is xhtml, but how do I tell the processor what namespace to use for the sourced document (the one read by document())? Just now it outputs <html xmlns="http://www.w3.org/1999/xhtml">
2
3994
by: toedipper | last post by:
Hello, The following bit of code does a preg match and does something if true (sets $browser to ppcie) Without using if then and else's how do I code it so it does not equal what it is testing for? So if it does not find ppc in the $agent then it does something else/sets it to something else? $agent = getenv("HTTP_USER_AGENT");
4
1997
by: system7designs | last post by:
I don't know preg's that well, can anyone tell me how to write a regular expression that will select everything BUT files/folders that begin with ._ or __?(that's period-underscore and underscore underscore)
0
950
by: rufus | last post by:
I have some text to parse. I dont want to match link text or text inside paragraphs of class=tab. All other text should be matched. Here is the text: ********** This text will match<a href="">This text wont match</a>This text will match<p class=tab>This text wont match</p><p class=other>This text will match</p>This text will match<a href="">This text wont match</a>This text will match. **********
1
1967
by: terence.parker | last post by:
I am trying to do a search through some data, more specifically HTML, to extract data from it. So for example I may have: <b>Title:</b<em>This is a title</em> <b>Name:</b<em>Fred</em> I wish to grab the data "This is a title" and "Fred" against their corresponding headings in an array (e.g. $array = "This is a title") .... but the key doesn't matter, that need not come from the regexp but I can do manually.
2
12264
by: ameshkin | last post by:
This script I wrote works with tables, td's and div's, but not with style tags. Can anyone figure out the regular expression for finding <styletags. The trick is that sometimes its not just <style Its <style type="text/css"> Basically, i want to take the information in between the style content from any url <?php
5
1397
by: monomaniac21 | last post by:
hi all what is the preg for capitals in a word to be replaced by that word preceded by a space? i need to be able to do this in preg: thisWord := this Word AnotherExample := Another Example
3
2278
moishy
by: moishy | last post by:
If I wanted to match for instance, all characters that are not in <TAGS>, I would search for all ">ANYTHING<". But how do I make that "ANYTHING"? What will be the PREG for absolutely ANY characer? (except ">" and "<")
0
9730
marktang
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...
0
10651
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, 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...
0
10392
jinu1996
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...
1
10403
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,...
0
9208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7671
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 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...
0
6893
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();...
0
5555
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4341
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

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.