The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 25, 2012, 10:22:59 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Regex: Pulling out a match that isn't a number  (Read 1880 times)
cdc
Expert
****
Offline Offline

Posts: 105


View Profile
« on: June 05, 2007, 11:49:17 AM »

I'm actually doing this using PHP's preg_match_all, but I think a regex is a regex is a regex so I posted it here.

I'm trying to pull some info off a page. There are several instances of this info spread across the page, and some of them are numbers. I'm not interested in the numbers.

So, here's what I would do if I were interested in the numbers:

/\<tag\>(.+?)\<\/tag\>/

Which would pull all the information between <tag></tag> on the page. However, I want to remove the ones that are a number so I naively tried this:

/\<tag\>[^0-9]\<\/tag\>/

What am I missing?
Logged

Will code for food.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 9896



View Profile
« Reply #1 on: June 05, 2007, 11:54:58 AM »

Hey C - would you please post an example of what you WOULD like to grab as well as what you wouldn't? That'd be just a bit easier to see.

Thanks!
/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
dirk
Global Moderator
Expert
*****
Offline Offline

Posts: 416


View Profile
« Reply #2 on: June 05, 2007, 01:13:57 PM »

Hi cdc,

your example would match: <tag>a</tag>
but not: <tag>ab</tag>

So I would change the regex to:

/\<tag\>[^0-9]+?\<\/tag\>/





Logged
cdc
Expert
****
Offline Offline

Posts: 105


View Profile
« Reply #3 on: June 05, 2007, 03:07:52 PM »

Thanks, dirk, works like a charm.

Just for completeness, here's an example of something I'd like it to match:

<tag>hello</tag>

Here's an example of what I don't want to match:

<tag>1</tag>

So I want to grab everything in the "tag" tag that's NOT a number.
Logged

Will code for food.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 9896



View Profile
« Reply #4 on: June 05, 2007, 03:10:43 PM »

Sheesh - really was that simple eh?
Sorry C - thought it'd be more complex if you posted some demos

/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
crypt
Rookie
**
Offline Offline

Posts: 12


View Profile
« Reply #5 on: June 05, 2007, 05:28:43 PM »

\D is the same as [^0-9]
\d is the same as [0-9]

\W is the same as [^a-zA-Z0-9]
\w is the same as [a-zA-Z0-9]

i thought there was one for [a-zA-Z], but i don't remember ever using it  Roll Eyes
Logged

No links in signatures please
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!