Matching special tags

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

Matching special tags

Post by socket1 »

I've read quite a bit about RegEx and I still can't figure it out to a point where I can make things work.
This post is similar to my previous one a while ago viewtopic.php?f=38&t=100032

I am trying to match all occurrences of something like [elementname] in a document.
It needs to be able to accept A-Z a-z 0-9 and _ characters of any length from 0 and greater.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Matching special tags

Post by prometheuzz »

This regex matches those tags:

Code: Select all

\[\w+]
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

Re: Matching special tags

Post by socket1 »

I tried using your regex and I get :
Warning: preg_match_all() [function.preg-match-all]: Delimiter must not be alphanumeric or backslash in F:\htdocs\scripts\run_code.php(4) : eval()'d code on line 1

this is what I used to test it:

Code: Select all

preg_match_all('\[\w+]', 'this is a [test] [string] full of[things]the regex should match', $matches);
print_r($matches);
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Matching special tags

Post by prometheuzz »

socket1 wrote:I tried using your regex and I get :
Warning: preg_match_all() [function.preg-match-all]: Delimiter must not be alphanumeric or backslash in F:\htdocs\scripts\run_code.php(4) : eval()'d code on line 1

this is what I used to test it:

Code: Select all

preg_match_all('\[\w+]', 'this is a [test] [string] full of[things]the regex should match', $matches);
print_r($matches);
You didn't wrap my suggested regex pattern with delimiters. I presume you know what regex delimiters are? If not, Google for it!
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

Re: Matching special tags

Post by socket1 »

Ahhhhh /\[\w+]/ my bad
I really don't undestand this RegEx stuff.

Thank you.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Matching special tags

Post by prometheuzz »

socket1 wrote:Ahhhhh /\[\w+]/ my bad
I really don't undestand this RegEx stuff.

Thank you.
Well, if you're using them from time to time, I recommend learning them.

You're welcome, of course.
Post Reply