Need help with regular expression

  • Thread starter Thread starter moondaddy
  • Start date Start date
M

moondaddy

I need to rename file names with a specific naming convention which includes
adding a SKU number at the end of the file name and I would like to use a
regular expression to do this, but I don't even know where to start on this
one.


For example say my file name is MyFileName.jpg and my SKU number is 1234. I
need to rename the file to
MyFileName---1234.jpg where the sku number gets prefixed with "---".
However, since there will be a large volume of file names to process and I
don't have any control of the original name coming in, I may have to clean
the name a bit first. For example the file name could possibly end with a
"-" or " -" or "_" or " _" in which case I would need to strip these off
first.

Can anyone help me with a regular expression to do this?

Thanks in advance.
 
Hi,

Thanks for posting in the community.

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you wants to write an regular
expression to replace the string as below
{"myfile1 _.jpg", "myfile2 -.jpg", "myfile3_.jpg", "myfile4-.jpg"}
to the string as below.
myfile1--0381.jpg
myfile2--85f3.jpg
myfile3--ef28.jpg
myfile4--8eaf.jpg
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

Here the SKUNumber is just for demostration, you may change to meet your
request.

Public Sub Main()
Dim TestStrings() As String = New String() {"myfile _.jpg", "myfile
-.jpg", "myfile_.jpg", "myfile-.jpg"}
Dim text As String
For i As Integer = 0 To TestStrings.Length - 1
text = TestStrings(i)
System.Console.WriteLine("text=[" & text & "]")
Dim r As Regex = New Regex("(.*)([ ]?[-_])(\.jpg)",
RegexOptions.IgnoreCase)
Dim SKUNumber As String = Right(Guid.NewGuid().ToString(), 4)
Dim m As Match = r.Match(text)
text = RTrim(m.Groups.Item(1).ToString()) & "--" & SKUNumber &
m.Groups.Item(3).ToString()
Console.WriteLine(text)
Next
End Sub

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
I think you understand, but I'll try to clearify one more time what the
original string may look like:

A typical string would be something like "myfile.jpg" and I want to add a
suffix of 3 dashes plus the sku number so it would look like this: (with a
sku number of 1234)

"myfile---1234.jpg"

However, I want to take into consideration that a user might submit a file
name with trailing dashes or underscores, and maybe even empty spaces before
or inbetween the dashes or underscores like these samples:
myfile -.jpg
myfile--.jpg
myfile _.jpg
ect.
so i would like to strip off any non-aphanumeric charictors after "myfile"
and before ".jpg", then insert the "---1234".

Does the change your regular expression?
 
Hi,

Based on my understanding, the strings you wants to filter consists of
three part.
1. myfile( it consists of alphabet and number only)
2. the filtered string( it consists of the combination of "_", "-" and " ")
3. .jpg

you hope the result string will be myfile---1234.jpg.

If so, here goes the code.
Public Sub Main()
Dim TestStrings() As String = New String() {"myfile1 _.jpg",
"myfile2 -.jpg", "myfile3_.jpg", "myfile4-.jpg", "myfile5--.jpg"}
Dim text As String
For i As Integer = 0 To TestStrings.Length - 1
text = TestStrings(i)
System.Console.WriteLine("text=[" & text & "]")
Dim r As Regex = New Regex("([a-z0-9]*)([- _]*)(\.jpg)",
RegexOptions.IgnoreCase)
Dim SKUNumber As String = Right(Guid.NewGuid().ToString(), 4)
Dim m As Match = r.Match(text)
text = RTrim(m.Groups.Item(1).ToString()) & "---" & SKUNumber &
m.Groups.Item(3).ToString()
Console.WriteLine(text)
Next
End Sub

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top