Current location: Hot Scripts Forums » Programming Languages » PHP » help with preg_match


help with preg_match

Reply
  #1 (permalink)  
Old 08-10-05, 07:00 PM
maddude maddude is offline
Newbie Coder
 
Join Date: Aug 2005
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
help with preg_match

hey guys i got this thing i need help with im using phps function:
PHP Code:

preg_match 

and i want to know what all the symbols are and what their used for could you help guys?.
Reply With Quote
  #2 (permalink)  
Old 08-10-05, 07:24 PM
DetroitGuy DetroitGuy is offline
Newbie Coder
 
Join Date: Jun 2005
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Reply With Quote
  #3 (permalink)  
Old 08-10-05, 07:39 PM
maddude maddude is offline
Newbie Coder
 
Join Date: Aug 2005
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
that doesnt explain symbols like "[" and "^" and stuff used in preg_match just its use.
Reply With Quote
  #4 (permalink)  
Old 08-10-05, 08:18 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by maddude
that doesnt explain symbols like "[" and "^" and stuff used in preg_match just its use.
Modifiers
i case-insensitive pattern matching.
g global replace, or replace all
m Treat string as multiple lines. That is, change ``^'' and ``$'' from matching at only the very start or end of the string to the start or end of any line anywhere within the string
s Treat string as single line. That is, change ``.'' to match any character whatsoever, even a newline, which it normally would not match.
x Extend your pattern's legibility by permitting whitespace and comments.

Special Characters
The following should be escaped if you are trying to match that character

\ ^ . $ | ( ) [ ]
* + ? { } ,

Special Character Definitions
\ Quote the next metacharacter
^ Match the beginning of the line
. Match any character (except newline)
$ Match the end of the line (or before newline at the end)
| Alternation
() Grouping
[] Character class
* Match 0 or more times
+ Match 1 or more times
? Match 1 or 0 times
{n} Match exactly n times
{n,} Match at least n times
{n,m} Match at least n but not more than m times
More Special Character Stuff
\t tab (HT, TAB)
\n newline (LF, NL)
\r return (CR)
\f form feed (FF)
\a alarm (bell) (BEL)
\e escape (think troff) (ESC)
\033 octal char (think of a PDP-11)
\x1B hex char
\c[ control char
\l lowercase next char (think vi)
\u uppercase next char (think vi)
\L lowercase till \E (think vi)
\U uppercase till \E (think vi)
\E end case modification (think vi)
\Q quote (disable) pattern metacharacters till \E
Even More Special Characters
\w Match a "word" character (alphanumeric plus "_")
\W Match a non-word character
\s Match a whitespace character
\S Match a non-whitespace character
\d Match a digit character
\D Match a non-digit character
\b Match a word boundary
\B Match a non-(word boundary)
\A Match only at beginning of string
\Z Match only at end of string, or before newline at the end
\z Match only at end of string
\G Match only where previous m//g left off (works only with /g)
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #5 (permalink)  
Old 08-10-05, 09:22 PM
maddude maddude is offline
Newbie Coder
 
Join Date: Aug 2005
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
but what if i dont know how many letters a variable is in length, how do i use preg_match to make sure some characters are certain type and the rest are a certain type, without knowing how many characters are in the string.
Reply With Quote
  #6 (permalink)  
Old 08-12-05, 12:38 AM
maddude maddude is offline
Newbie Coder
 
Join Date: Aug 2005
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
well? any help?
Reply With Quote
  #7 (permalink)  
Old 08-12-05, 12:42 AM
darkfreak's Avatar
darkfreak darkfreak is offline
Newbie Coder
 
Join Date: Jun 2004
Location: Kuopio, Finland, Europe
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
Give us an example.
Reply With Quote
  #8 (permalink)  
Old 08-12-05, 01:47 AM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by maddude
but what if i dont know how many letters a variable is in length, how do i use preg_match to make sure some characters are certain type and the rest are a certain type, without knowing how many characters are in the string.
If you looked at what I posted you'd probably have an idea already.

The "+" and "*" match 1 or more or none or more, respectively. The "." matches any character. If you have a string like:

<title>some text here</title>

You can match on the text between the tags like this:

<title>(.*)</title>

That will match anything or nothing. To match only if there is some text there (at least one char), you could do this:

<title>(.+)</title>

Want to match a number? Do this: \d
Match 5 numbers, a dash, and two more numbers? Do this: \d\d\d\d\d-\d\d

Match a to z and 1 to 9: [A-Za-z1-9]

Look the list over and experiment. There's also tons of stuff on the net about regular expressions.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #9 (permalink)  
Old 08-12-05, 01:58 AM
darkfreak's Avatar
darkfreak darkfreak is offline
Newbie Coder
 
Join Date: Jun 2004
Location: Kuopio, Finland, Europe
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
Reply With Quote
  #10 (permalink)  
Old 08-17-05, 12:51 PM
maddude maddude is offline
Newbie Coder
 
Join Date: Aug 2005
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by End User
Modifiers
i case-insensitive pattern matching.
g global replace, or replace all
m Treat string as multiple lines. That is, change ``^'' and ``$'' from matching at only the very start or end of the string to the start or end of any line anywhere within the string
s Treat string as single line. That is, change ``.'' to match any character whatsoever, even a newline, which it normally would not match.
x Extend your pattern's legibility by permitting whitespace and comments.

Special Characters
The following should be escaped if you are trying to match that character

\ ^ . $ | ( ) [ ]
* + ? { } ,

Special Character Definitions
\ Quote the next metacharacter
^ Match the beginning of the line
. Match any character (except newline)
$ Match the end of the line (or before newline at the end)
| Alternation
() Grouping
[] Character class
* Match 0 or more times
+ Match 1 or more times
? Match 1 or 0 times
{n} Match exactly n times
{n,} Match at least n times
{n,m} Match at least n but not more than m times
More Special Character Stuff
\t tab (HT, TAB)
\n newline (LF, NL)
\r return (CR)
\f form feed (FF)
\a alarm (bell) (BEL)
\e escape (think troff) (ESC)
\033 octal char (think of a PDP-11)
\x1B hex char
\c[ control char
\l lowercase next char (think vi)
\u uppercase next char (think vi)
\L lowercase till \E (think vi)
\U uppercase till \E (think vi)
\E end case modification (think vi)
\Q quote (disable) pattern metacharacters till \E
Even More Special Characters
\w Match a "word" character (alphanumeric plus "_")
\W Match a non-word character
\s Match a whitespace character
\S Match a non-whitespace character
\d Match a digit character
\D Match a non-digit character
\b Match a word boundary
\B Match a non-(word boundary)
\A Match only at beginning of string
\Z Match only at end of string, or before newline at the end
\z Match only at end of string
\G Match only where previous m//g left off (works only with /g)
right so how exactly do i say it must have only 1 A-Z character in between <title> and </title> tags. and no other characters in between.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 07:01 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.