0

I have two videos format first one only dashes and second one underscores and dashes. I need figure out format by Sitecore powershell script from specific path.

Format One Video Images (dashes):
video-thumb-5226977037001.jpg
video-still-5226977037001.jpg

Format Two Video Images (underscores and dashes):
video_thumb-4721086006001
video_still-4721086006001
3
  • Are familiar with regular expressions in c sharp? You can evaluate those same expressions in powershell. Commented Sep 16, 2024 at 15:17
  • @Michael West Im not familiar with Powershell and Regex Commented Sep 16, 2024 at 15:21
  • @Hasanshali I have uploaded the answer below to check when the string contains both the characters dashes and underscore then it returns true and similalry for the dashes character scenario. Let me know in case you have any doubts. Commented Sep 16, 2024 at 16:55

2 Answers 2

1

You can use below Powershell snippet to check whether the string contains underscore and dashes or the string contains only dashes.

$underScoreDashRegex = "^([a-zA-Z0-9-._]*[-]+[a-zA-Z0-9-._]*[_]+[a-zA-Z0-9-._]*)|([a-zA-Z0-9-._]*[_]+[a-zA-Z0-9-._]*[-]+[a-zA-Z0-9-._]*)$"
$dashRegex = "^([a-zA-Z0-9-.]*[-]+[a-zA-Z0-9-.]*)$"

$input = "video_thumb-4721086006001"

if($input -match $underScoreDashRegex){
    Write-Host "The input string contains underScore as well as dashes"
}elseif($input -match $dashRegex){
    Write-Host "The input string contains only dashes"
}else{
    Write-Host "The string does not matches any of the criterias"  
} 

This is the output returned through above script.

enter image description here

Using above Powershell snippet, the first condition will only be true when string contains both the characters i.e. undersore as well as dashes(in any order) and second condition returns true only when string contains dash characters.

Hope this helps!!! Let me know in case you have any queries.

1
  • @Hasanshali Glad to hear that. Please accept the answer in order for helping other people in the community that may face similar issues. Commented Sep 16, 2024 at 19:18
2

To check for string validation using regular expressions in Sitecore PowerShell, you can use the -match operator.

Here is an example of how to use it.

# Input string to validate
$stringInput = "YourStringHere"

# Define the regular expression pattern for validation
# Example to check alphanumeric string with 5-10 characters
$pattern = "^[a-zA-Z0-9]{5,10}$"  

# Perform validation check
if ($stringInput -match $pattern) {
    Write-Host "The input string is valid."
} else {
    Write-Host "The input string is not valid."
}

For underscore and dashes you can use this pattern.

$pattern = "^[a-zA-Z0-9_-]+$"

For dashes only you can use this.

$pattern = "^[a-zA-Z0-9-]+$"

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.