locked
check a string only contains certain characters RRS feed

  • Question

  • Hi

    I need to check a string value only contains certain characters AND only contains one of each value

    I was hoping I could use something like regex for this but am struggling

    The string can contain the letters D A T S O U or nothing at all, and it can only contain one of each letter, the order is not important


    Darren Rose

    Friday, November 9, 2018 10:49 AM

Answers

  • If each of allowed letters must appear zero or one time, then check this expression:

       ^((?<c>[DATSOU])(?!.*\k<c>))+$

    Make it case-insensitive, if needed.


    • Marked as answer by wingers Tuesday, November 13, 2018 4:50 PM
    Saturday, November 10, 2018 10:10 AM
  • [...]

    Can you perhaps tell me a) how I make it case-insensitive and b) how I could change it so nothing at all is also a valid entry


    Check this:

       (?i)^((?<c>[DATSOU])(?!.*\k<c>))*$

    • Marked as answer by wingers Tuesday, November 13, 2018 4:50 PM
    Saturday, November 10, 2018 6:38 PM

All replies

  • Hello, 

    only contains certain characters

    Simple solution for - string.Split() - with parameters included all letters except "D A T S O U".

    single item as result - string contain only those symbols.

    AND only contains one of each value

    Count characters or use LINQ to count what ever is more than 1.


    Sincerely, Highly skilled coding monkey.


    Friday, November 9, 2018 10:53 AM
  • Edit: misread the question, this is not a solution

    Index of case insensitive

    If someString.IndexOf("D", StringComparison.OrdinalIgnoreCase) > -1 Then
        Console.WriteLine("Yes")
    End If

    All chars

    Dim someString As String = "D A T S O U"
    Dim results =
            (
                From T In (From c In someString.ToCharArray()
                            Group c By c Into Group Select New With {.Item = c, .Occurrences = Group.Count, .Code = Asc(c)}
            )).ToList()
    
    For Each item In results
        Console.WriteLine($"'{item.Item}' x {item.Occurrences}")
    Next
    Dim oneChar = results.FirstOrDefault(Function(c) c.Item = "A"c)
    If oneChar IsNot Nothing Then
        Console.WriteLine(oneChar.Code)
    End If
    If results.Any(Function(c) c.Item = "A"c) Then
        Console.WriteLine(oneChar.Code)
    End If




    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Friday, November 9, 2018 12:25 PM
  • thank you both for your replies

    Was hoping to use regex and think this may be working for me to check only contains allowed letters

     Dim pattern As String = "^[DATSOU]*$"
                Dim reg As New Regex(pattern)
    
                If reg.IsMatch(Value) Then
                   ' okay
                Else
                    MessageBox.Show("Field can only contain characters DATSOU", "Invalid input", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
    Just need to check only contains 0 or 1 of each character


    Darren Rose

    Friday, November 9, 2018 12:29 PM
  • If each of allowed letters must appear zero or one time, then check this expression:

       ^((?<c>[DATSOU])(?!.*\k<c>))+$

    Make it case-insensitive, if needed.


    • Marked as answer by wingers Tuesday, November 13, 2018 4:50 PM
    Saturday, November 10, 2018 10:10 AM
  • If each of allowed letters must appear zero or one time, then check this expression:

       ^((?<c>[DATSOU])(?!.*\k<c>))+$

    Make it case-insensitive, if needed.


    Hi Viorel_

    Thank you for you response

    Can you perhaps tell me a) how I make it case-insensitive and b) how I could change it so nothing at all is also a valid entry


    Darren Rose

    Saturday, November 10, 2018 5:27 PM
  • [...]

    Can you perhaps tell me a) how I make it case-insensitive and b) how I could change it so nothing at all is also a valid entry


    Check this:

       (?i)^((?<c>[DATSOU])(?!.*\k<c>))*$

    • Marked as answer by wingers Tuesday, November 13, 2018 4:50 PM
    Saturday, November 10, 2018 6:38 PM
  • [...]

    Can you perhaps tell me a) how I make it case-insensitive and b) how I could change it so nothing at all is also a valid entry


    Check this:

       (?i)^((?<c>[DATSOU])(?!.*\k<c>))*$

    Thank you very much Viorel_ - that works perfectly

    Wish I could throw together regex expressions as well as you can

    Perhaps I can pick your brain then:-

    I have another field where I have to enter a time range in format of 1115-2118

    so needs to be two valid times separate by a -

    Could I use regex to validate this string as well somehow?


    Darren Rose

    Saturday, November 10, 2018 6:52 PM
  • [...]

    Can you perhaps tell me a) how I make it case-insensitive and b) how I could change it so nothing at all is also a valid entry


    Check this:

       (?i)^((?<c>[DATSOU])(?!.*\k<c>))*$

    Thank you very much Viorel_ - that works perfectly

    Wish I could throw together regex expressions as well as you can

    Perhaps I can pick your brain then:-

    I have another field where I have to enter a time range in format of 1115-2118

    so needs to be two valid times separate by a -

    Could I use regex to validate this string as well somehow?


    Darren Rose

    Hi,

    your mean is  input 1115-2118

    "1115-2118"

    or input  a range in 1115-2118?

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Alex-KSGZ Monday, November 12, 2018 3:15 AM
    Monday, November 12, 2018 3:15 AM
  • [...]

    Can you perhaps tell me a) how I make it case-insensitive and b) how I could change it so nothing at all is also a valid entry


    Check this:

       (?i)^((?<c>[DATSOU])(?!.*\k<c>))*$

    Thank you very much Viorel_ - that works perfectly

    Wish I could throw together regex expressions as well as you can

    Perhaps I can pick your brain then:-

    I have another field where I have to enter a time range in format of 1115-2118

    so needs to be two valid times separate by a -

    Could I use regex to validate this string as well somehow?


    Darren Rose

    Hi,

    your mean is  input 1115-2118

    "1115-2118"

    or input  a range in 1115-2118?

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    I mean a time range as I said in post e.g. hhmm-hhmm


    Darren Rose

    Monday, November 12, 2018 9:51 AM