473,569 Members | 2,617 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2170
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($s tring,'.')<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
2059
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 make the ESC key quit the called function when ever the user inputs data. The description was : ESC drops back to the main menu in case the user gets...
7
2916
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 in Nederlands, maar Engels is ook goed. Thnx, Rensjuh
16
2609
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 string is directly generated in the code. But it doesn't works if I wait for this inside a 'while' loop. This is the simple code: global stringZVEI
51
8233
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 code? many thx!
104
5451
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 gives no titlebar etc but the form borders don't look 3D. In case I haven't explained what I want well, I want a form that looks like a button...
8
2318
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 calculates loan amortization schedules, writes the data to a text file, and then upon user prompt, the program will display the created file and...
7
3127
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
2492
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 the array is an object itself but what is this syntax of the consecutive double quotes inside the brackets ?
9
1732
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 behavior. char *returnValueFromIniFile(char *iniFilePath,char *keyIntheFile)
0
7615
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...
0
7924
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. ...
0
8130
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...
1
7677
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...
1
5514
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...
0
5219
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...
0
3653
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...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.