#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Posts
    9
    Rep Power
    0

    Help with regular expression, example inside


    I am a novice at regular expressions and I need some help with a real easy one for a pro.
    I am using PHP and the preg_replace function. I want to edit out the text that is in between the '()' and the brackets themselves.

    Here is an example:
    Yankees (New York) VS White Sox (Chicago)
    I want the result to be:
    Yankees VS White Sox

    Thanks in advance!
  2. #2
  3. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    937
    Replace
    Code:
    ([^)]+)
    with an empty string.

    Details: http://www.php.net/preg_replace
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Posts
    9
    Rep Power
    0
    Originally Posted by prometheuzz
    Replace
    Code:
    ([^)]+)
    with an empty string.

    Details: http://www.php.net/preg_replace
    I appreciate the help. I was unable to get it to work, can you give me an example of the full preg_replace function with the code that will delete out the data I am trying to omit.

    Sorry to be a pain in the butt, I actually have done some regex work but it has been a while and I am lost at the moment.
  6. #4
  7. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    937
    Originally Posted by johnevegas
    I appreciate the help. I was unable to get it to work, can you give me an example of the full preg_replace function with the code that will delete out the data I am trying to omit.

    Sorry to be a pain in the butt, I actually have done some regex work but it has been a while and I am lost at the moment.
    Did you read the link I posted, or at least part of the link? It explains in great detail how to use the preg_replace function. I already showed you the pattern, the rest is trivial if you are prepared to do just a bit of reading on your own.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Posts
    9
    Rep Power
    0
    Originally Posted by prometheuzz
    Did you read the link I posted, or at least part of the link? It explains in great detail how to use the preg_replace function. I already showed you the pattern, the rest is trivial if you are prepared to do just a bit of reading on your own.
    I read the link. I know the basic structure of the preg_replace. If you read my initial post, I mentioned that is what I would be using the regular expression for.

    I tried the code and I could not get it to work. I am obviously doing something wrong and all I really need is the working example of the PHP code. I understand that you are trying to get me to learn for myself and I appreciate it. I always want to learn but I am backlogged and all I need is just the solution, not to have to take the time to work on brushing up and learning my regular expressions. If you do not want to help me maybe someone out there can simply post the preg_replace function correctly for me.
  10. #6
  11. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    937
    If you really did try it, you should have posted your best effort here on the forum and explained what was wrong with it. I am positive that I, or someone else would easily have spotted the error and posted an explanation of how to fix it. But later in your post you say "but I am backlogged and all I need is just the solution" which sounds a bit contradictory to your earlier claim.

    Anyway, if all you need is the solution, I will make way for the spoon-feeders-squad.

    Good luck.
  12. #7
  13. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    937
    Sorry to say this, but I really don't believe you read the link. I mean, it is littered with examples and all you have to do is copy and paste some example from it, use the pattern I posted in my first reply, and all works just fine.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Posts
    9
    Rep Power
    0
    I did not post my example(s) because they were not working and no doubt make me look stupid. Here is what I was doing to test:
    echo preg_replace ("/([^)]+)/", "", "Yankees (New York) VS White Sox (Chicago)");

    It is not working and I do not know why. I do not know enough to see if the code you put up was even correct and how sure can I be? If you just put up a working function, I would see if it worked or not. Thanks for trying to help but I am not sure why you try to make something so simple (to you) become so difficult.

    BTW, I DID read that page way before I even posted here and have read it many times in the past.
  16. #9
  17. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    937
    Originally Posted by johnevegas
    I did not post my example(s) because they were not working and no doubt make me look stupid.
    I can guarentee you that will not happen quickly. If you post your attempt(s) and cleary indicate what the problem is, no one will think you're stupid: honestly!


    Originally Posted by johnevegas
    Here is what I was doing to test:
    echo preg_replace ("/([^)]+)/", "", "Yankees (New York) VS White Sox (Chicago)");

    It is not working and I do not know why. I do not know enough to see if the code you put up was even correct and how sure can I be? If you just put up a working function, I would see if it worked or not.
    My apologies: the pattern I posted does not work. There's a nice saying that applies here: "even monkeys fall from trees". I don't know how I missed that! The '(' and ')' are special characters in the regex language. So you need to escape them:

    PHP Code:
    echo preg_replace ("/\([^)]+\)/""""Yankees (New York) VS White Sox (Chicago)"); 
    Originally Posted by johnevegas
    Thanks for trying to help but I am not sure why you try to make something so simple (to you) become so difficult.
    I am a natural sceptic: if I do not see any evidence that leads me to believe someone put in a bit of effort on their own, I am reluctant to help. Perhaps it is not always justified, but hey, I choose how I spend my free time helping others (or trying to!)

    Originally Posted by johnevegas
    BTW, I DID read that page way before I even posted here and have read it many times in the past.
    Fair enough.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Posts
    9
    Rep Power
    0
    Thanks a lot. I did try over and over even before I posted and I was thinking that the pattern was off but I do not know enough to see it exactly. I rarely ask for help but also know when I am beaten.

    You are a true stand up individual for posting again.

    Comparing the other pattern to the new pattern, I actually can see the difference and I think that I learned from that too.

    I truly appreciate you help!!
  20. #11
  21. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    937
    Originally Posted by johnevegas
    Thanks a lot. I did try over and over even before I posted and I was thinking that the pattern was off but I do not know enough to see it exactly. I rarely ask for help but also know when I am beaten.

    You are a true stand up individual for posting again.

    Comparing the other pattern to the new pattern, I actually can see the difference and I think that I learned from that too.

    I truly appreciate you help!!
    No problem. In case you haven't decrypted the pattern (yet), here's a short explanation:

    Code:
    \(     // match a '('
    [^)]   // match any character other than ')' (since ')' is not a special character inside square brackets, there's no need to escape it)
    +      // match the previous line one ore more times
    \)     // match a ')'
    So, in plain English this would be: "match an opening parenthesis followed by one or more characters other than a closing parenthesis followed by a closing parenthesis".
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Posts
    9
    Rep Power
    0
    Nice, thanks a lot!

IMN logo majestic logo threadwatch logo seochat tools logo