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. 14 2023
frizzle wrote:
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
Is hello-_there ok?
Is hello_-there ok?
Is _hello-there ok?
If the answer to the above three questions is no, then the following
should do the trick. Note that this implies that the final character
could be a - or _:
if (preg_match('/^([a-z0-9][-_]?)+$/', $string)) { ... }
Csaba Gabor from New York
frizzle wrote:
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.
What you need is a lookahead and lookbehind assertion on the dash and
underscore, stating that they're acceptable only if there're letters in
front and behind them:
/^(?:[a-z]|(?<=[a-z])[-_](?=[a-z]))+$/
Chung Leong wrote:
frizzle wrote:
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.
What you need is a lookahead and lookbehind assertion on the dash and
underscore, stating that they're acceptable only if there're letters in
front and behind them:
/^(?:[a-z]|(?<=[a-z])[-_](?=[a-z]))+$/
/^(?:[a-z]|(?<=[a-z])[-_](?=[a-z]))+$/
wowowow, could you explain a little on this ?
like the : and ?<= parts
(i assume 0-9 should still be included??)
Frizzle.
frizzle wrote:
Chung Leong wrote:
frizzle wrote:
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.
What you need is a lookahead and lookbehind assertion on the dash and
underscore, stating that they're acceptable only if there're letters in
front and behind them:
/^(?:[a-z]|(?<=[a-z])[-_](?=[a-z]))+$/
/^(?:[a-z]|(?<=[a-z])[-_](?=[a-z]))+$/
wowowow, could you explain a little on this ?
like the : and ?<= parts
(i assume 0-9 should still be included??)
Frizzle.
Still curious after the explanation, but just letting you know it works
axactly as it should ..
Frizzle.
frizzle wrote:
Chung Leong wrote:
>frizzle wrote:
>>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.
What you need is a lookahead and lookbehind assertion on the dash and underscore, stating that they're acceptable only if there're letters in front and behind them:
/^(?:[a-z]|(?<=[a-z])[-_](?=[a-z]))+$/
wowowow, could you explain a little on this ?
like the : and ?<= parts
non-capturing group (usefull when you just want to match, and don't need the
exact matched portion): http://www.regular-expressions.info/brackets.html
positive lookbehind: http://www.regular-expressions.info/lookaround.html
$regex ='/ #opening delimiter
^ #start of string
(?: #start of non-capturing group
[a-z] #any character between a and z
| #OR
(?<= #start of positive lookbehind (is preceeded by..)
[a-z] #any character between a and z
) #end of positive lookbehind
[-_] #character - or _ (not incorrect, but probably better
to [_\-],[_-] or [\-_]
(?= #start of positive lookahead
[a-z] #any character between a and z
) #end of positive lookahead
) #end of non-capturing group
+ #1 or more times, greedy
$ #end of string
/x';
Human translation:
The entire(1) string consists of 1 or more (2) characters [a-z] and possibly
the single characters _ or - enclosed by characters in the range [a-z].
(1) by achoring them with ^.....$
(2) by +
(i assume 0-9 should still be included??)
If you want that, yes, just change every [a-z] to [a-z0-9].
Use the /i modifier if you want a match to be case-insensitive.
Grtz,
--
Rik Wasmus
Rik wrote:
$regex ='/ #opening delimiter
^ #start of string
(?: #start of non-capturing group
[a-z] #any character between a and z
| #OR
(?<= #start of positive lookbehind (is preceeded
by..) [a-z] #any character between a and z
) #end of positive lookbehind
[-_] #character - or _ (not incorrect, but probably
better to [_\-],[_-] or [\-_]
(?= #start of positive lookahead
[a-z] #any character between a and z
) #end of positive lookahead
) #end of non-capturing group
+ #1 or more times, greedy
$ #end of string
/x';
It just occured to me that, allthough a wonderfull example:
$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.
Grtz,
--
Rik Wasmus
Rik wrote:
Rik wrote:
$regex ='/ #opening delimiter
^ #start of string
(?: #start of non-capturing group
[a-z] #any character between a and z
| #OR
(?<= #start of positive lookbehind (is preceeded
by..) [a-z] #any character between a and z
) #end of positive lookbehind
[-_] #character - or _ (not incorrect, but probably
better to [_\-],[_-] or [\-_]
(?= #start of positive lookahead
[a-z] #any character between a and z
) #end of positive lookahead
) #end of non-capturing group
+ #1 or more times, greedy
$ #end of string
/x';
It just occured to me that, allthough a wonderfull example:
$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.
Grtz,
--
Rik Wasmus
Wow, thanks for the explanation!
Nice link there as well. Going right into my bookmarks.
Frizzle.
Rik wrote:
It just occured to me that, allthough a wonderfull example:
$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.
Chung Leong wrote:
Rik wrote:
It just occured to me that, allthough a wonderfull example:
$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
So i used Chung's option.
Frizzle. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.)
|
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">
|
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");
|
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)
|
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.
**********
| |
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.
|
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
|
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
|
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 "<")
|
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: 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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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: 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: 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...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |