![]() ![]() \\ is used for a literal back slash character. Take special properties away from special characters: \. would be used to represent a literal dot character.\ is the escape character for RegEx, the escape character has two jobs: is used to represent any single character, aside from a newline, so it will feel very similar to the windows wildcard ? Today we’ll ease in with some of the basics to get us going, but later we will expand on these and see some other options we have. I mentioned the most important thing is to understand the symbols. Last post we talked a little bit about the basics of RegEx and its uses. We will take a look at some other symbols and some little tricks we can use grouping and ?for in future posts!Īs always, don’t forget to rate, comment and share! Let me know what you think of the content and what topics you’d like to see me blog about in the future.Welcome back to the RegEx guide. "Last Name" = $īrenda Seamon Brotherwood Duffan you’ve had fun playing with RegEx so far. This scales nicely for looping though! $data = "Brenda Seamon, Joeann Brotherwood, Jake Duffan " Its a bit more work, since we need to keep track of which result we are on in a group of matches. Let’s try grabbing the groups using $data = "Brenda Seamon " Notice $matches is a hash table, and our group names become the keys. This makes our pattern start to look really bananas if we saw it without context $pattern = $data = "Brenda Seamon " " -f $matches,$matches,$matches,$matches All match: Brenda Seamon name: BrendaĮmail: can also name these groups using ?inside of the parens. Now that we know it works, lets try grouping up the pieces we want by putting parens around the first, last and email sections: $pattern = "(\w )\s (\w )\s " $data = "Brenda Seamon " We can see that words with our test: $data = "Brenda Seamon " I used this one: $pattern = "\w \s \w \s " Let’s start by finding a pattern that gets all of our data. They’re all in our data, and we can use grouping to pull them out individually. This time, we want to grab the First, Last, and Email to use for things. ![]() Maybe we grab some data by copy/pasting out of outlook and it looks like this: Brenda Seamon If its in a large block of text we might want to use RegEx to extract it like we have before. Each individual group then gets pulled out in numerical order. The full pattern will always match as group 0, which is why we were typing $matches to start. Grouping can be done by wrapping sections of your pattern in parenthesis. ![]() The data I want is all there, but I plan to use pieces of it for different things.Now I can match the unique piece and rip out what I want to use. The data I want isn’t unique on its own, but the data around it is.Grouping is a way that we can logically break up our extraction. Now it all looks a lot sleeker, go ! Grouping $regex = all matches and display the value property $file = get-content "$PSScriptRoot\MOCK_DATA.txt" -raw Let’s take a look at how we might modify the email match from the earlier post to use this: #grab our data as one big blob (-raw) Value : thing we care about is the value property, but you’ll notice it even tells you the starting character and how many characters long it is. If we take a look at some sample data that it returns, we can see that we actually get a pretty rich match object: Groups : This means that if there is more than 1 match per line we can still get it! With ::matches()we can condense all that and it could work on a big blob of text instead of just a list of individual lines. Using $matches like we did in the previous posts means we have to write a lot of looping and if statements. The data type has some cool static members, but we’re mostly going to play with the plural method matches(,) if you don’t know what static members are you can check this post or this help data.Ī lot of the time, when we work with RegEx we are using it to extract everything that matches our pattern in a large amount of data. This week, we will be learning a new way to leverage our patterns for data extraction and how to rip our extracted data into pieces we care about. ![]() Last time we talked about the basic symbols we plan to use as our foundation. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |