473,395 Members | 1,680 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,395 software developers and data experts.

Beginner question: detecting specified character in string

Hello, trying to create logic tests against a string allowing someone
to enter a certain character once or twice into a form consecutively,
but not twice seperated between other characters....

so,

hello##there (ok)
hello#there (ok)
#hello#there (no)
##hello#there (no)
###hellothere (no)

How would I do this? I was looking at strstr and some others, but not
sure. I have many challenges like this ahead, so hopefully someone
building this will help my mind figure out other designs.

Thanks so much!

-Julie

Jul 17 '05 #1
8 2157
wanted to add a little depth to the above, this is what i'm trying to
incorporate the above logic into:

(This so far will only accept upper and lower case letters)

if(ereg("[^a-zA-Z]", $string)){

[some error message]

} else {

[$string passes validation....]

}

....so, confirming, I am trying to allow percisely # or ##, nothing
more. confirming ## can't be two or more #'s split up, two #'s only.
(or one) :)

Jul 17 '05 #2
Julia Briggs wrote:
Hello, trying to create logic tests against a string allowing someone
to enter a certain character once or twice into a form consecutively,
but not twice seperated between other characters....

so,

hello##there (ok)
hello#there (ok)
#hello#there (no)
##hello#there (no)
###hellothere (no)


preg_match('`^[^#]*#{0,2}[^#]*$`',$subject)

Explanation: The pattern matches zero or more of any
character except '#', then zero, one or two '#'s, then zero
or more of any character except '#' again.

Have a good weekend!

--
Jock
Jul 17 '05 #3
Thanks a million. So I can see how it delineates in the mix, how would
I add into that preg_match the following:

1) Allow all A-Z and a-z characters, with no special characters except
a period and apostrophe
2) Allow all numbers

Could you show this as two examples, one with and without the numbers
matching?

Jul 17 '05 #4
Julia Briggs wrote:
So I can see how it delineates in the mix, how would
I add into that preg_match the following:
I don't follow. What do you mean by 'add into'?
1) Allow all A-Z and a-z characters, with no special characters except
a period and apostrophe
Together those characters form the character class [a-zA-
Z.']. Do you want to substitute that for the '#' in the
previous pattern? If so, then:

`^[^a-zA-Z.']*[a-zA-Z.']{0,2}[^a-zA-Z.']*$`

Explanation: match zero or more characters except any from
your character class (a-z, A-Z, full stop and apostrophe);
then match zero, one or two characters from your character
class; then, finally, match zero or more characters except
any from your character class.

Or do you mean something else?
2) Allow all numbers


For decimal numbers just add \d or [0-9]; e.g.,

`^[^a-zA-Z\d.']*[a-zA-Z\d.']{0,2}[^a-zA-Z\d.']*$`

You must escape all the single-quotes if the pattern string
is single-quoted.

Slainte!

--
Jock
Jul 17 '05 #5
Sorry, for clarification, I meant introduce by "add". I want to keep
the # and its original expression, and also allow all
uppercase/lowercase and a period and apostrophe.

Sincerely,

Julia

John Dunlop wrote:
Julia Briggs wrote:
So I can see how it delineates in the mix, how would
I add into that preg_match the following:


I don't follow. What do you mean by 'add into'?
1) Allow all A-Z and a-z characters, with no special characters except a period and apostrophe


Together those characters form the character class [a-zA-
Z.']. Do you want to substitute that for the '#' in the
previous pattern? If so, then:

`^[^a-zA-Z.']*[a-zA-Z.']{0,2}[^a-zA-Z.']*$`

Explanation: match zero or more characters except any from
your character class (a-z, A-Z, full stop and apostrophe);
then match zero, one or two characters from your character
class; then, finally, match zero or more characters except
any from your character class.

Or do you mean something else?
2) Allow all numbers


For decimal numbers just add \d or [0-9]; e.g.,

`^[^a-zA-Z\d.']*[a-zA-Z\d.']{0,2}[^a-zA-Z\d.']*$`

You must escape all the single-quotes if the pattern string
is single-quoted.

Slainte!

--
Jock


Jul 17 '05 #6
Julia Briggs wrote:
I want to keep the # and its original expression, and also
allow all uppercase/lowercase and a period and apostrophe.


The original pattern did that.

But if you want to match only a-z, A-Z, full stops and
apostrophes, and an optional number sign ('#') appearing
once or twice together -- in other words, the original
pattern but with the characters other than the number sign
restricted to the set [a-zA-Z.'] -- then

`^[a-zA-Z.']*#{0,2}[a-zA-Z.']*$`

Is that what you're after?

--
Jock
Jul 17 '05 #7
Correct criteria, but blows up on execution with my code. :/ - I think
it is because of the introduced ' symbol. Does that need to be
escaped, or something else? I took it out and it works. This will not
permit spaces. I need those. One more small detail, I would love it
if it could work this way -- to have all of this criteria as we have it
(plus spaces) and only allow one instance of a period/full stop too.

Here is what I have codewise so far that is blowing up because of the '

if(preg_match('`^[a-zA-Z.']*-{0,2}[a-zA-Z.']*$`',$string)) { [etc...]

Thank you from the bottom of my heart for this. You extended
explanations are great. Very professional, helpful and first class
John.

Sincerely,

Julia.

John Dunlop wrote:
Julia Briggs wrote:
I want to keep the # and its original expression, and also
allow all uppercase/lowercase and a period and apostrophe.


The original pattern did that.

But if you want to match only a-z, A-Z, full stops and
apostrophes, and an optional number sign ('#') appearing
once or twice together -- in other words, the original
pattern but with the characters other than the number sign
restricted to the set [a-zA-Z.'] -- then

`^[a-zA-Z.']*#{0,2}[a-zA-Z.']*$`

Is that what you're after?

--
Jock


Jul 17 '05 #8
Julia Briggs wrote:
Correct criteria, but blows up on execution with my code. :/ - I think
it is because of the introduced ' symbol. Does that need to be
escaped, or something else? I took it out and it works.
If the pattern string is enclosed in single-quotes, then,
yes, single-quotes in the pattern itself must be escaped so
that they're not interpreted as closing the string. You
would typically escape them with backslashes.
This will not permit spaces. I need those.
Simply change your pattern to

`^[a-zA-Z.' ]*#{0,2}[a-zA-Z.' ]*$`

(with single-quotes escaped if required)
One more small detail, I would love it if it could work this way -- to
have all of this criteria as we have it (plus spaces) and only allow one
instance of a period/full stop too.
I can't think how to do that in the same pattern. You could
split it into two patterns, with the one most likely to fail
coming first. Or you could use the string functions.

http://www.php.net/manual/en/ref.strings.php
Here is what I have codewise so far that is blowing up because of the '

if(preg_match('`^[a-zA-Z.']*-{0,2}[a-zA-Z.']*$`',$string)) { [etc...]


Change your expression to

if(
preg_match(
'`^[a-zA-Z.\' ]*-{0,2}[a-zA-Z.\' ]*$`',
$string)
&&
substr_count($string,'.')<2)

There is an additional expression which evaluates to true
only if there are fewer than two full stops in $string.

http://www.php.net/manual/en/function.substr-count.php

Have a relaxing Sunday.

--
Jock
Jul 17 '05 #9

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

Similar topics

8
by: Foxy Kav | last post by:
Hi everyone, Im currently doing first year UNI, taking a programming course in C++, for one project i have to create a simple array manipulator... that i have done, but i cant figure out how to...
7
by: Rensjuh | last post by:
Hello, does someone have / know a good C++ tutorial for beginnners? I would prefer Dutch, but English is also fine. Hoi, heeft / kent iemand nog een goede C++ tutorial voor beginners? Het liefste...
16
by: dario | last post by:
Hi, Im new on phyton programming. On my GPRS modem with embedded Phyton 1.5.2+ version, I have to receive a string from serial port and after send this one enclosed in an e-mail. All OK if the...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
104
by: Colin McGuire | last post by:
Hi, is there a way to show a form without a titlebar (and therefore no control box/minimize box/title etc) but still have it appear looking like 3D? The property FormBorderStyle to None - this...
8
by: AG | last post by:
Hello, This is my first post to this group, and on top of that I am a beginner. So please direct me to another group if this post seems out of place.... I have recently written a program which...
7
by: fool | last post by:
Dear group, Extract the integer value present in a given string. So I tried the following: int main(void) { int val; char *data; data = malloc(sizeof *data); if(data)
2
by: JJA | last post by:
I'm looking at some code I do not understand: var icons = new Array(); icons = new GIcon(); icons.image = "somefilename.png"; I read this as an array of icons is being built. An element of...
9
by: somenath | last post by:
Hi All, I was going through one piece of code which is written by an experience programmer and it is working fine. But I think the use of "strstr" is not proper because it may show undefined...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
tracyyun
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...

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.