Pattern
-
Could you help me write a pattern for Regex. I need to look for file name with this pattern: string(no number; only 100 characters)-6digitnumber_string
what have you done so far? and can you clarify with some positive and negative examples? :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
Could you help me write a pattern for Regex. I need to look for file name with this pattern: string(no number; only 100 characters)-6digitnumber_string
Off the top of my head, this one should suffice:
([a-zA-z]{100}\d{6}[a-zA-Z]{1,})
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
-
Off the top of my head, this one should suffice:
([a-zA-z]{100}\d{6}[a-zA-Z]{1,})
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
-
Could you help me write a pattern for Regex. I need to look for file name with this pattern: string(no number; only 100 characters)-6digitnumber_string
-\d{6}_
:)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
examples: Testing-123456_filename.xls - valid My-1234568_filename.txt- not valid (because 7 digits) Testring-123456-filename.dat- not valid( missing _) filename.txt - not valid( missing _ and - ) Testring_123456_filename.dat- not valid( missing -)
This doesn't match what you specified originally. You stated that it should be 100 characters, but My-, for instance, isn't. So, which set of rules are correct?
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
-
This doesn't match what you specified originally. You stated that it should be 100 characters, but My-, for instance, isn't. So, which set of rules are correct?
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
-
-\d{6}_
:)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
If I used you pattern I am getting all invalid returns Here my code and test cases:
Public Function Verify(ByVal myFileName As String) Dim Pattern As String = "-d{6}_" '"([a-zA-z]{100}\d{6}[a-zA-z]{1,})" Dim MyRegex As New RegularExpressions.Regex(Pattern, RegularExpressions.RegexOptions.IgnoreCase) Return MyRegex.IsMatch(myFileName, 0) End Function
Testing scenario:If Verify("Granic-012345_Test-Granic.txt") Then ' should be valid MessageBox.Show("valid") Else MessageBox.Show("Not valid") End If If Verify("TESTTING-123456-Norman_NDtest1.dat") Then ' should be not valid MessageBox.Show("valid") Else MessageBox.Show("Not valid") End If If Verify("GRANIC-123456_jayson.xls") Then ' should be not valid MessageBox.Show("valid") Else MessageBox.Show("Not valid") End If If Verify("incoming-testfile.txt") Then ' should be not valid MessageBox.Show("valid") Else MessageBox.Show("Not valid") End If If Verify("Testing-474948_filename.txt") Then ' should be valid MessageBox.Show("valid") Else MessageBox.Show("Not valid") End If
-
If I used you pattern I am getting all invalid returns Here my code and test cases:
Public Function Verify(ByVal myFileName As String) Dim Pattern As String = "-d{6}_" '"([a-zA-z]{100}\d{6}[a-zA-z]{1,})" Dim MyRegex As New RegularExpressions.Regex(Pattern, RegularExpressions.RegexOptions.IgnoreCase) Return MyRegex.IsMatch(myFileName, 0) End Function
Testing scenario:If Verify("Granic-012345_Test-Granic.txt") Then ' should be valid MessageBox.Show("valid") Else MessageBox.Show("Not valid") End If If Verify("TESTTING-123456-Norman_NDtest1.dat") Then ' should be not valid MessageBox.Show("valid") Else MessageBox.Show("Not valid") End If If Verify("GRANIC-123456_jayson.xls") Then ' should be not valid MessageBox.Show("valid") Else MessageBox.Show("Not valid") End If If Verify("incoming-testfile.txt") Then ' should be not valid MessageBox.Show("valid") Else MessageBox.Show("Not valid") End If If Verify("Testing-474948_filename.txt") Then ' should be valid MessageBox.Show("valid") Else MessageBox.Show("Not valid") End If
that is a useless message: a lot of code, but no results. and code should be in PRE tags, not CODE tags. it would help if you would copy my suggestion correctly. :|
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
Then try this:
[a-zA-Z]{1,100}-\d{6}_[a-zA-Z]{1,}
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
-
Then try this:
[a-zA-Z]{1,100}-\d{6}_[a-zA-Z]{1,}
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
-
that is a useless message: a lot of code, but no results. and code should be in PRE tags, not CODE tags. it would help if you would copy my suggestion correctly. :|
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
but if he kept the \ then there would be compile time errors, so it is best to just not put it in than to work out why it causes an issue, right? :~
I may or may not be responsible for my own actions
Wrong. if it is to be a string literal (not stated explicitly), then it would be OK in some languages such as VB.NET (not specified), it would not be OK in some others such as C, and it would be OK in C# only when using a preceeding @ sign or doubling the \. And that is exactly why I didn't put any double quotes at all, I only specified what the content of the string had to be. I trust people know their programming language of choice well enough to turn that into a string literal if that is what they need. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
Wrong. if it is to be a string literal (not stated explicitly), then it would be OK in some languages such as VB.NET (not specified), it would not be OK in some others such as C, and it would be OK in C# only when using a preceeding @ sign or doubling the \. And that is exactly why I didn't put any double quotes at all, I only specified what the content of the string had to be. I trust people know their programming language of choice well enough to turn that into a string literal if that is what they need. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
Could you help me write a pattern for Regex. I need to look for file name with this pattern: string(no number; only 100 characters)-6digitnumber_string
It would be better for us if you could come up with some examples ? As I understood from your given information, this is required to you:
^\D{5}\-\d{6}_\D{1,}$
So it should match with this kind of samples:
aaaaa-666666_a