January 1, 2012 at 12:00 am #5125MemberPoints: 0Rank: Member
by surge3333 at 2012-09-19 08:42:51
Iâ€™m trying to extract a portion of a string, however I donâ€™t know the beginning position of the piece I want to extract, or the length of that piece.
Essentially Iâ€™m looking for the word â€œWeekâ€, and want to extract anything after that.
The string could look like this â€“
Different stuff here-Week_10am
Once I find â€œWeekâ€, I want to extract everything onwards.
by poshoholic at 2012-09-19 09:46:20
Regular expressions are your friend for this.
'Some-text-here-Week_abc_8am" -replace '^.*week(.*)$','$1'
by surge3333 at 2012-09-19 12:19:09
Great, that works.
I've been trying to use regex to do something similiar with no luck.
I need to extract everything after the last "_" in a string.
The string could be – a_bc_d_e_xyz
How to I get it to pick up from the last "_" onwards?
by poshoholic at 2012-09-19 12:27:40
Here's a regex that will allow you to do that:
'a_bc_d_e_xyz' -replace '^.*(_[^_]+)$','$1'
The important part is what appears in the round brackets. They identify that you want to find (and capture — that's what the brackets do) an underscore followed by one or more non-underscore characters at the end of a string. The $1 allows you to replace the entire string with the item you found.
by willsteele at 2012-09-19 16:01:45
What does the whole '$1', portion of the -replace syntax here mean? I have seen Rob use it as well, but, don't quite know what it means.
by poshoholic at 2012-09-19 18:06:17
In regular expressions, while you search for a pattern match you have the option of capturing a string that matches either some portion of a pattern or an entire pattern. You identify what you want to capture by surrounding the appropriate part of the regex pattern in round brackets. You can also capture multiple strings, using multiple sets of round brackets. When you capture strings, on -replace the first string is referenced by $1, the second by $2, and so on. Note that these are not PowerShell variables. They're regex matches. So if you want to do a replace using double-quotes for the replacement string, you need to escape the dollar-sign before the regex captured value index.
Looking at the specific regex replace above, it searches a string for an underscore (_) followed by one or more non-underscore characters ([^_]+) at the end of a string '$'. In that match, it identifies that there may be zero or more characters (any character) leading up to that match in the string (^.*). The round brackets around the underscore and the regex that identifies one or more non-underscore characters indicate we want to capture that portion of the string if a match is found, and then we want to replace the entire string (from ^ to $) with the first captured match ('$1').
Here's another easier example. Consider this string:
$fullyQualifiedUsername = 'POSHSTUDIOS\Poshoholic'
If I wanted to identify the domain and username in that string using regex (I wouldn't use regex in practice for this, but it's just an example), I could do this:
$domain = $fullyQualifiedUsername -replace '^(.+)\\.+$','$1'
$username = $fullyQualifiedUsername -replace '^.+\\(.+)$','$1'
I'm simply using brackets to identify a portion of a string I care about, and replacing the entire string with just that portion.
The topic ‘SubString without knowing starting position or length’ is closed to new replies.