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

[HELP] Regex ... regular expression really is my nemisis!

Discussion in 'General Programming Chat' started by carlx, Mar 24, 2012.

  1. carlx

    carlx Registered Member

    Joined:
    Dec 28, 2008
    Messages:
    95
    Likes Received:
    88
    I have always hated regex and i just cant seem to get a working expression to grab the information i want! Help would be greatly appreciated!

    The HTML code i am trying to grab:
    Code:
                        <p>Ending:
                            25 Mar 12
                        </p>
    
    I want to grab the date "25 Mar 12" and not have any whitespace with it. ... multiline regex just doesnt seem to like me at all and i have been playing with it for an hour now!

    If you can help me out, that would be awesome! ... also anybody have a "idiots guide to regex" that they could direct me to, i need to learn it.... but i hate it so much!
     
    Last edited: Mar 25, 2012
  2. *zap*

    *zap* Regular Member

    Joined:
    Apr 7, 2008
    Messages:
    349
    Likes Received:
    98
    Occupation:
    none
    Location:
    Uk
    (?<=Ending\:\r)\d{2}\s.*\s\d{2}(?=\r\<\/p\>)


    maybe?
     
  3. carlx

    carlx Registered Member

    Joined:
    Dec 28, 2008
    Messages:
    95
    Likes Received:
    88
    Nope, that isn't grabbing anything :(
     
  4. *zap*

    *zap* Regular Member

    Joined:
    Apr 7, 2008
    Messages:
    349
    Likes Received:
    98
    Occupation:
    none
    Location:
    Uk
    works with .net i don't know enough about other languages and regex sorry
     
  5. carlx

    carlx Registered Member

    Joined:
    Dec 28, 2008
    Messages:
    95
    Likes Received:
    88
    I'm working in .Net and its not returning anything! ... i've had enough of this bloody regex!
     
  6. *zap*

    *zap* Regular Member

    Joined:
    Apr 7, 2008
    Messages:
    349
    Likes Received:
    98
    Occupation:
    none
    Location:
    Uk
    Then try

    (?<=Ending\:\r)\d{2}\s.*\s\d{2}\r(?=\<\/p\>)

    You can test them at
    Code:
    http://gskinner.com/RegExr/
     
    • Thanks Thanks x 1
  7. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,602
    Likes Received:
    11,171
    Occupation:
    Pusillanimous Knitter
    Location:
    Buenos Aires
    Code:
    Dim ResultString As String
    Try
    	ResultString = Regex.Match(SubjectString, "<p>Ending:\r\n(.*+)\r\n</p>", RegexOptions.Multiline).Groups(1).Value
    Catch ex As ArgumentException
    	'Syntax error in the regular expression
    End Try
    
    Code:
    string ResultString = null;
    try {
    	ResultString = Regex.Match(SubjectString, "<p>Ending:\\r\\n(.*+)\\r\\n</p>",
    		RegexOptions.Multiline).Groups[1].Value;
    } catch (ArgumentException ex) {
    	// Syntax error in the regular expression
    }
    
    Without dot matching new line.
     
    • Thanks Thanks x 1
  8. Nitros

    Nitros Power Member

    Joined:
    Jan 30, 2009
    Messages:
    580
    Likes Received:
    298
    try this: jslab.dk/tools.regex.php
     
    • Thanks Thanks x 1
  9. andee

    andee Regular Member

    Joined:
    Jul 24, 2010
    Messages:
    218
    Likes Received:
    83
    I had this problem, its the new line character replace it with ""

    or use the getstringinbetween function thats floating around, I remember someone already posted it in the vb section , or ive got an example one if you need it.
     
  10. carlx

    carlx Registered Member

    Joined:
    Dec 28, 2008
    Messages:
    95
    Likes Received:
    88
    I do appologise, it was my mistake!

    The code pasted in the origional post has no white spaces, the actualy code can be found here:
    HTML:
    http://www.competitions-time.co.uk/Computers-Comps/Win-Dell-XPS-15Z-Laptop-3540
    - see the source code's formatting (i don't want to paste it again and make another mistake)
     
  11. carlx

    carlx Registered Member

    Joined:
    Dec 28, 2008
    Messages:
    95
    Likes Received:
    88
    Still cant get this to work! going to sleep or im going to shoot somebody!
     
  12. andee

    andee Regular Member

    Joined:
    Jul 24, 2010
    Messages:
    218
    Likes Received:
    83
  13. BlackSEOWizard

    BlackSEOWizard Regular Member

    Joined:
    Apr 2, 2010
    Messages:
    303
    Likes Received:
    442
    Give this a try, should solve your problem.

    Code:
    (\d{2}\s\w{3}\s\d{2})(</p>)
    You want to grab $1
     
    • Thanks Thanks x 1
  14. BlackSEOWizard

    BlackSEOWizard Regular Member

    Joined:
    Apr 2, 2010
    Messages:
    303
    Likes Received:
    442
    As for learning RegEX, I would recommend the O'Reilly book and getting RegEX Buddy. Both are invaluable, but still most people are never very good at RegEX that attempt to learn it.
     
    • Thanks Thanks x 1
  15. carlx

    carlx Registered Member

    Joined:
    Dec 28, 2008
    Messages:
    95
    Likes Received:
    88
    Thank you, i certainly need to learn it because it is crucial for a lot of things i do, until now i've always managed to get it working, but yours did the trick, perfect first time!

    +Rep & Thanks given.
     
  16. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,602
    Likes Received:
    11,171
    Occupation:
    Pusillanimous Knitter
    Location:
    Buenos Aires
    VB

    Code:
    Dim ResultString As String
    Try
    	ResultString = Regex.Match(SubjectString, "<p>\s*Ending:\s*(.*)\s+</p>").Groups(1).Value
    Catch ex As ArgumentException
    	'Syntax error in the regular expression
    End Try
    C#

    Code:
    string ResultString = null;
    try {
    	ResultString = Regex.Match(SubjectString, "<p>\\s*Ending:\\s*(.*)\\s+</p>").Groups[1].Value;
    } catch (ArgumentException ex) {
    	// Syntax error in the regular expression
    }
    
    
     
    • Thanks Thanks x 1
  17. hatemachine

    hatemachine Regular Member

    Joined:
    Jan 14, 2011
    Messages:
    271
    Likes Received:
    1,137