A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearHow do I match a square bracket literal using RegEx? - page 1
User InfoPosts
How do I match a square bracket literal using RegEx?#1
What's the regex to match a square bracket? I'm using \\] in a pattern in eregi_replace, but it doesn't seem to be able to find a ]...

posted date: 2008-12-08 02:52:00


Re: How do I match a square bracket literal using RegEx?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-08 02:52:01


Re: How do I match a square bracket literal using RegEx?#3
Works flawlessly:<?php $hay = "ab]cd"; echo eregi_replace("\]", "e", $hay);?>Output:abecd

posted date: 2008-12-08 03:01:00


Re: How do I match a square bracket literal using RegEx?#4
\] is correct, but note that PHP itself ALSO has \ as an escape character, so you might have to use \\[ (or a different kind of string literal).

posted date: 2008-12-08 03:02:00


Re: How do I match a square bracket literal using RegEx?#5
You problem may come from the fact you are using eregi_replace with the first parameter enclosed in simple quotes: '\['In double quotes, though, it could works well depending on the context, since it changes the way the parameter is passed to the function (simple quotes just pass the string without any interpretation, hence the need to double to "\" character).Here, if "\[" is interpreted as an escape character, you still need to double "\".Note: based on your comment, you may try the regex<\s*(?:br|p)\s*\/?\s*\>\s*\[in order to detect a [ right after a <br>or a <p>

posted date: 2008-12-08 03:29:00


Re: How do I match a square bracket literal using RegEx?#6
In .Net you escape special characters by adding up a backslash; "\" meaning it would become; "["...Though since you normally do this in string literals you would either have to do something like this;@"\["or something like this;"\\["

posted date: 2008-12-08 03:48:00


Re: How do I match a square bracket literal using RegEx?#7
I'm actually trying to match something thats either on a new line (or a new html line - could be right after <p> too) that has an @ at the beginning with nothing before it...

posted date: 2008-12-08 04:28:00


Re: How do I match a square bracket literal using RegEx?#8
I'm using PHP, so I'm not sure this would be of any help to me...

posted date: 2008-12-08 04:29:00


Re: How do I match a square bracket literal using RegEx?#9
I don't follow you... I am using double quotes in my code and it isn't working.

posted date: 2008-12-08 04:30:00


Re: How do I match a square bracket literal using RegEx?#10
I would have say the reverse, in single quotes, backslash is a character like the others, except before backslash and single quote, IIRC.

posted date: 2008-12-08 04:38:00


Re: How do I match a square bracket literal using RegEx?#11
Just tried Bombe's code, works fine with '\]', '\\]', "\]" and "\\]"... Actually, ] doesn't need to be escaped if there is no [ before it.

posted date: 2008-12-08 04:50:00


Re: How do I match a square bracket literal using RegEx?#12
You don't need to escape it: if isolated, a ] is treated as a regular character.Tested with eregi_replace and preg_replace.[ is another beast, you have to escape it. Looks like single and double quotes, single or double escape are all treated the same by PHP, for both regex families.Perhaps your problem is elsewhere in your expression, you should give it in full.

posted date: 2008-12-08 04:56:00


select page: « 1 2 »
Copyright ©2008-2017 www.momige.com, all rights reserved.