Jump to content
Larry Ullman's Book Forums

Help With Regular Expression Please


Recommended Posts

I need a regular expression for this shortcode

[image file=name.ext]

The shortcode is image and there will only be one attribute, file. Obviously there might be spaces either side of the equals sign and the name and ext can be a variable length made up of numbers, characters, dashes and underscores.

 

Initially to keep it simple I only allowed numbers and letters in the name and ext and no spaces either side of the equals sign. I've tried several options but none are working. How do you represent the [ and ] in a regex. My attempts at escaping them with a double backslash haven't worked. Here's what I tried

/^(//[image file=])+([\w.])+(\\]{1})$

I also tried using a single backslash to escape the brackets, but that didn't work either. Thanks for any suggestions.

Link to comment
Share on other sites

Hey, margaux.

I think I am either completely misunderstanding what you want your regex to do, or the regex you have provided is way off.

As far as I can tell, the following regex (in PCRE syntax) should serve you well:

 

'/^\[\s*image\s+file\s*=\s*[\w-]+\.[\w-]+\s*\]$/'

 

It's a bit confusing, but it allows for the following:

1) A single [

2) Any number of (or maybe zero) whitespace characters before the literal "image"

3) The literal "image"

4) One or more whitespace characters between "image" and "file"

5) The literal "file"

6) Any number of (or maybe zero) whitespace characters before =

7) the literal "="

8) Any number of (or maybe zero) whitespace characters after =

9) One or more of any number, letter, dash or underscore (You said "character", but that might be a bit too broad.)

10) A literal . (must be escaped)

11) One or more of any number, letter, dash or underscore

12) One or more whitespace characters

13) A single ]

 

Does that give you what you want?

Link to comment
Share on other sites

Hey HartleySan - that is perfect! Just to confirm my understanding

 

\] = 1) A single [

\s* = 2) Any number of (or maybe zero) whitespace characters before the literal "image"

image = 3) The literal "image"

\s+ = 4) One or more whitespace characters between "image" and "file"

file = 5) The literal "file"

\s* = 6) Any number of (or maybe zero) whitespace characters before =

= = 7) the literal "="

\s* = 8) Any number of (or maybe zero) whitespace characters after =

[\w-]+ =  9) One or more of any number, letter, dash or underscore (You said "character", but that might be a bit too broad.)

\. = 10) A literal . (must be escaped)

[\w-]+ = 11) One or more of any number, letter, dash or underscore

\s* = 12) Zero or more whitespace characters

\] = 13) A single ]

 

^ = start of string

$ = end of string so anything after the ] will not be processed.

the whole expression must be between backslashes

 

I was using + to indicate something totally different! Also I thought it was necessary to enclose each part or subpattern in parantheses but I guess that is not the case. When would you use parantheses. Thanks again,you have really helped my understanding of how to build regular expressions.

Link to comment
Share on other sites

There are two uses for parentheses in regexes, as explained by the following:

http://www.regular-expressions.info/brackets.html

 

1) To apply a repetition operator (i.e., *, + or ?) to a group of characters.

2) To create a backreference. Backreferences are useful for search-and-replace operations using regexes.

 

I think the above link explains everything pretty well.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...