|
-
Jan 29th, 2011, 02:53 AM
#1
Thread Starter
Fanatic Member
[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

-
Jan 29th, 2011, 03:29 AM
#2
Banned
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
-
Jan 29th, 2011, 04:01 AM
#3
Fanatic Member
Re: Regex to get string.
vb Code:
Dim contents As String = Regex.Match("string you posted above goes here", "(?<=#pStyle03X1 {).*?(?=})", RegexOptions.IgnoreCase).Value MsgBox(contents)
If I helped you out, please take the time to rate me 
-
Jan 29th, 2011, 04:11 AM
#4
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]
-
Jan 29th, 2011, 04:17 AM
#5
Fanatic Member
Re: Regex to get string.
 Originally Posted by dynamic_sysop
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:
Dim mCollect As MatchCollection = Regex.Matches("inputstr", "(?<=#pStyle\w*\s{).*?(?=})", RegexOptions.IgnoreCase)
For each m As Match in mCollect
MsgBox(m.Value)
Next
If I helped you out, please take the time to rate me 
-
Jan 29th, 2011, 04:57 AM
#6
Re: Regex to get string.
 Originally Posted by J-Deezy
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|