1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Why isn't this regex working?? I can grab src but it gets all of them??

Discussion in 'General Programming Chat' started by simpleonline1234, Nov 16, 2011.

  1. simpleonline1234

    simpleonline1234 Junior Member

    Joined:
    Jan 26, 2010
    Messages:
    169
    Likes Received:
    13
    Here is the current regex

    Code:
    "src=[\""\'](http:\/\/|\.\/|\/)?\w+(\.\w+)*(\/\w+(\.\w+)?)*(\/|\?\w*=\w*(&\w*=\w*)*)?[\""\']>"

    What I am trying to do is to grab element that is:

    Code:
    <script type="text/javascript" src="http://www.mysite="I need to grab everything inside these quotes">
    With the current Regex I am able to grab the value but it also grabs all the "src" elements on the page.

    Code:
    src="http://www.mysite="I need to grab everything inside these quotes">
    What I am attempting to do is to only get this one line that includes the

    <script and then the src="">

    And then just get the src="info from hrere"

    How can I edit the current regex code to include <script type="text/javascript"


    Thanks
     
  2. meatro

    meatro BANNED BANNED

    Joined:
    Nov 21, 2009
    Messages:
    568
    Likes Received:
    997
    (?<=src=")(.*?)(?=")

    (?<=src=") = Text on left, match but exclude from results.
    (.*?) = any character, any number of repititions.
    (?=") = Text on right, match but exclude.

    This:
    <script type="text/javascript" src="http://www.mysite="I need to grab everything inside these quotes">

    Returns this:
    http://www.mysite=

    :)

    [edit]

    Also, if you want to make sure you get only javascript SRC, not images and other elements that use the SRC command... Just add javascript to the regex. Like this:

    (?<=<script type="text/javascript" src=")(.*?)(?=")
     
    Last edited: Nov 16, 2011
  3. simpleonline1234

    simpleonline1234 Junior Member

    Joined:
    Jan 26, 2010
    Messages:
    169
    Likes Received:
    13
    Oh wow..thank Meatro..that works...

    Question though....what if you wanted to include it in the results??

    (?=") = Text on right, match but exclude.


    Would I just remove something from example you gave me?

    Hey thanks again.
     
  4. meatro

    meatro BANNED BANNED

    Joined:
    Nov 21, 2009
    Messages:
    568
    Likes Received:
    997
    (?=").{1} ..the dot means any character AFTER what it's found. The {1} means only the one character. Change that to a 2 if you want the " and the >

    Returns: http://www.mysite="

    If you do (?=").+ ... this will match EVERYTHING on the line after what it's found. (as will (?=").*)
    So it would return: http://www.mysite="I need to grab everything inside these quotes">
     
  5. meatro

    meatro BANNED BANNED

    Joined:
    Nov 21, 2009
    Messages:
    568
    Likes Received:
    997
    Also, if you're working with RegEx, I suggest an app called Expresso. It's free and it has a design mode, so you can pretty much click and select what you're trying to do, it will write the expression for you and explain what it's doing. :)
     
    • Thanks Thanks x 2
  6. simpleonline1234

    simpleonline1234 Junior Member

    Joined:
    Jan 26, 2010
    Messages:
    169
    Likes Received:
    13
    Ah....man..that works awesome...sweet...thanks for the help...yeah I'm going to give Expresso a whirl...I tried regexbuddy but I was lost in translation trying to figure that out.
     
    • Thanks Thanks x 1