header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Get specific charaters
Last Post 15 Sep 2008 02:43 PM by alanrenouf. 10 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
alanrenoufUser is Offline
New Member
New Member
Posts:25

--
11 Sep 2008 09:39 AM  

Hi,

I am trying to get specfic information from a webpage which i have managed to get into a string, i now want to extract the information needed in the string, can anyone please help.

My string current equals: 

E:\test.txt:471:    [tr][th]Manager:  [/th]Joe Smith                                 [/td][/tr]

I have replaced the formatting with square brackets as it messed up this post.

What I want is the name that comes after the [/th] and nothing before or after that, if it helps the start character will always be the same number.

Basically what I want to end up with is

Joe Smith

 

Thanks

ShayUser is Offline
Basic Member
Basic Member
Posts:281
Avatar

--
11 Sep 2008 11:38 AM  
You can use positive lookbehind regular expression:

PS> $string = 'Manager:Joe Smith'
PS > [regex]::match($string,'(?<=Manager:).+').value
Joe Smith

More Info on this:
http://www.regular-expressions.info/lookaround.html

Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
alanrenoufUser is Offline
New Member
New Member
Posts:25

--
11 Sep 2008 11:48 AM  

Brilliant, thanks, I had read about regex but couldnt work out the formatting.

I am still left with the trailing spaces and then [/td][/tr] after the name, how can i get rid of these ?

Alan

Heard your name on the PowerScripting Podcast, good work.

ShayUser is Offline
Basic Member
Basic Member
Posts:281
Avatar

--
11 Sep 2008 11:58 AM  

Thnaks ;-)
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
alanrenoufUser is Offline
New Member
New Member
Posts:25

--
11 Sep 2008 12:03 PM  

Any ideas on the trailing spaces and [/td][/tr] ?

Thanks

ShayUser is Offline
Basic Member
Basic Member
Posts:281
Avatar

--
11 Sep 2008 12:06 PM  

Can you post a sample string? You didn't mention those in your post.

Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
alanrenoufUser is Offline
New Member
New Member
Posts:25

--
11 Sep 2008 12:14 PM  

In my first post

 

My string current equals: 

E:\test.txt:471:    [tr][th]Manager:  [/th]Joe Smith                                 [/td][/tr]

ShayUser is Offline
Basic Member
Basic Member
Posts:281
Avatar

--
11 Sep 2008 12:49 PM  
[regex]::match($string,'(?<=Manager:\s+\[/th\]).+(?=\s+\[)').value

Here's another option using select-string:

$string = 'E:\test.txt:471: [tr][th]Manager: [/th]Joe Smith [/td][/tr]'
$m = select-string -inputObject $string -pattern 'joe smith'
$m.matches[0].value






Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
alanrenoufUser is Offline
New Member
New Member
Posts:25

--
11 Sep 2008 12:52 PM  

Sorry should have mentioned, the reason Im doing this is to get the name, the name Joe Smith was just an example and will change in each of the strings.

Is there anyway of altering my new string after the regex you showed me to create a new string where regex gets only the fisrt two words ?

Thanks for your help.

ShayUser is Offline
Basic Member
Basic Member
Posts:281
Avatar

--
11 Sep 2008 01:11 PM  
Doesn't the following do it?

[regex]::match($string,'(?<=Manager:\s+\[/th\]).+(?=\s+\[)').value

Here's a break down of the pattern:

Give me all characters that begins with 'Manager:' followed by a closing TH tag and one or more spaces and ends with a one or more space characters followe by '['.

Makes sense?



Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
alanrenoufUser is Offline
New Member
New Member
Posts:25

--
15 Sep 2008 02:43 PM  

That worked great, thanks very much for your help and thanks for the explanation, that really helped.

 

Alan

You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer