Results 1 to 6 of 6

Thread: [RESOLVED] Regex to get string.

  1. #1

    Thread Starter
    Fanatic Member vijy's Avatar
    Join Date
    May 2007
    Location
    India
    Posts
    548

    Resolved [RESOLVED] Regex to get string.

    i am having a file like below.

    Code:
    .....
    .....
    #pStyle03X0 {XXX: 0px;YYYY: 144px;xxxxx:XXXXX: 1000;xxxxx: 498.00; xxxxx:714.00; }
    #pStyle03X1 {xxxxx: 0px;xxxxx: 0px;xxxxx: 0px;xxxxx: 0px;xxxxx: xxxxx 33; }
    #pStyle03X2 {xxxxx: 14px;xxxxx: 0px;xxxxx: 12px;xxxxx: 0px;xxxxx: xxxxx 33; }
    .....
    .....
    i need to extract the content inside the braces{} of #pStyle03X1.

    i tried some patterns but not getting any match.
    Visual Studio.net 2010
    If this post is useful, rate it


  2. #2
    Banned
    Join Date
    Mar 2009
    Posts
    764

    Re: Regex to get string.

    there is a user of the forum called alex user name stlaural i think he posted some regex source codes

  3. #3
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: Regex to get string.

    vb Code:
    1. Dim contents As String = Regex.Match("string you posted above goes here", "(?<=#pStyle03X1 {).*?(?=})", RegexOptions.IgnoreCase).Value
    2. MsgBox(contents)
    If I helped you out, please take the time to rate me

  4. #4
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Re: Regex to get string.

    because the pStyle varies, you would need to make a new regex pattern for each line with that code.
    you could do this also, which would avoid the pStyle issue...
    Code:
            '/// this pattern will search for the { and the stuff between and the }
            Dim strPattern As String = Regex.Escape("{") & "(.*)}"
    
            Dim strToSearch As String = "#pStyle03X0 {XXX: 0px;YYYY: 144px;xxxxx:XXXXX: 1000;xxxxx: 498.00; xxxxx:714.00; }"
    
            Dim rgx As New Regex(strPattern, RegexOptions.IgnoreCase)
    
            Dim strResult As String = String.Empty
            '/// this will trim the {} off each match as it's found...
            Dim charsToTrim As Char() = {"{", "}"}
    
            Dim rgxMatches As MatchCollection = rgx.Matches(strToSearch)
    
            For Each m As Match In rgxMatches
                strResult &= m.Value.Trim(charsToTrim) & Environment.NewLine
            Next
    
            MessageBox.Show(strResult)
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  5. #5
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: Regex to get string.

    Quote Originally Posted by dynamic_sysop View Post
    because the pStyle varies, you would need to make a new regex pattern for each line with that code.
    you could do this also, which would avoid the pStyle issue...
    Code:
            '/// this pattern will search for the { and the stuff between and the }
            Dim strPattern As String = Regex.Escape("{") & "(.*)}"
    
            Dim strToSearch As String = "#pStyle03X0 {XXX: 0px;YYYY: 144px;xxxxx:XXXXX: 1000;xxxxx: 498.00; xxxxx:714.00; }"
    
            Dim rgx As New Regex(strPattern, RegexOptions.IgnoreCase)
    
            Dim strResult As String = String.Empty
            '/// this will trim the {} off each match as it's found...
            Dim charsToTrim As Char() = {"{", "}"}
    
            Dim rgxMatches As MatchCollection = rgx.Matches(strToSearch)
    
            For Each m As Match In rgxMatches
                strResult &= m.Value.Trim(charsToTrim) & Environment.NewLine
            Next
    
            MessageBox.Show(strResult)
    He said he wanted the contents of "#pStyle03X1", not the contents of all of them. I answered his specific question. If you wanted to capture them all:

    vb Code:
    1. Dim mCollect As MatchCollection = Regex.Matches("inputstr", "(?<=#pStyle\w*\s{).*?(?=})", RegexOptions.IgnoreCase)
    2.  
    3. For each m As Match in mCollect
    4.     MsgBox(m.Value)
    5. Next
    If I helped you out, please take the time to rate me

  6. #6
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Re: Regex to get string.

    Quote Originally Posted by J-Deezy View Post
    He said he wanted the contents of "#pStyle03X1", not the contents of all of them. I answered his specific question. If you wanted to capture them all:
    been a long week, lucky for me i listened to the ad on the TV & i really am off to specsavers in a few minutes lol
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width