This topic contains 7 replies, has 4 voices, and was last updated by
November 14, 2016 at 7:56 am #57556
I am new to Regex world, below is my requirement.
$Output = " 2.1G /ofs01/prod/OFSFileStore_auto_1"
I need to extract the 2.1G from the data, so I did the below thing.
It gave me the output 2.1G which is perfect, but now the Condition is :
1. 2.1G Value should not be greater than 2.5G
2. It is not necessary that it will be in Gb only, it can be 385M or 385.27M like this
3. So first thing is I need to extract the (value+GB or MB). If its in GB then will start comparing else i will need to convert it to GB and then compare with 2.5 GB
So kindly help me to provide any regex or solution as per the points mentioned above.
November 14, 2016 at 8:49 am #57562
If you do it like this:
$Output -match "(\d+\.*\d+)([M|G])"
you will have the number in $Matches and the G or M in $Matches so you can check and proceed accordingly
November 14, 2016 at 9:27 am #57568
Olaf Thanks for the reply. but in case the Output doesn't have a decimal value. it will return False.
$Output = " 2G /ofs01/prod/OFSFileStore_auto_1"
# return false
Can u guide further on this.
November 14, 2016 at 9:27 am #57569ParticipantPoints: 57Rank: Member
While your solution should work for the OP I wanted to point out the possible misunderstanding of the  characters in yours and the OP's regex pattern. The square brackets are used to identify a range of characters or multiple characters to match. Therefore with the way both of you have written [M|G] the pipe character does not mean "or", rather it is a character that can be matched in the string. Instead of the square brackets simply use regular brackets/parenthesis to group characters in an "or" statement separated by the pipe character. To use your example I'd simply remove just the square brackets from your pattern to get the desired result.
I think I'm this specific case it may not be a problem, however you may get undesired results say if for example the is no M or G in the place you expect, and there is a pipe character further along in the string, with all digits before it. That may come up as a match with your regex pattern.
Otherwise I like your use of capture groups, and my only other comment would be to follow the capture group with a unique character that is known to immediately follow the desired characters to ensure you don't capture more than you expect. + and * can be "greedy" and match characters past where you intended. Hence there is also the ? Qualifier for + and * which tell them not to be "greedy" and stop at the first instance of the next character in your pattern.
I hope I've explained my point well enough as after reading it again it seems like gibberish but this happens a lot with regex...
November 14, 2016 at 9:58 am #57571
OK OK ... so this should work then
$Output -match "(\d+\.*\d*)(M|G)"
November 14, 2016 at 11:14 am #57572
Thanks Olaf, That worked fine 🙂
Can you suggest on this : https://powershell.org/forums/topic/extacting-capacity-info-using-powershell-regex/
November 14, 2016 at 11:24 am #57574
1. You're wlcome. 😉
2. Well I could ... but first – I don't understand your question completely (englich is not my native language as well) and second – I don't like to write scripts on request. ( Usually I get paid for that 😉 ) If you have some code to show where you have problems or errors you could show it here and we will try to help you. For prewritten scripts you can search here: Microsoft Technet Script Gallery
November 14, 2016 at 4:17 pm #57623
The topic ‘Regex in Powershell’ is closed to new replies.