Results 1 to 4 of 4

Thread: regex

  1. #1
    Join Date
    Jan 2007
    Location
    Baltimore, MD
    Beans
    841
    Distro
    Ubuntu

    regex

    is there a way in grep to match individual characters just once?

    for example, i'd like to grep for the letters a, b and c in any order, but each letter should only appear once (or not at all) in the line.

    can it be done?

  2. #2
    Join Date
    Oct 2007
    Location
    Vienna, Europe
    Beans
    77

    Re: regex

    Quote Originally Posted by finer recliner View Post
    is there a way in grep to match individual characters just once?

    for example, i'd like to grep for the letters a, b and c in any order, but each letter should only appear once (or not at all) in the line.

    can it be done?
    for the letter 'a':
    Code:
    /^[^a]*[a]?[^a]*$/

    all characters in one regex, I don't know
    Last edited by a9bejo; March 6th, 2008 at 05:29 PM.

  3. #3
    Join Date
    Sep 2006
    Beans
    2,914

    Re: regex

    Quote Originally Posted by finer recliner View Post
    is there a way in grep to match individual characters just once?

    for example, i'd like to grep for the letters a, b and c in any order, but each letter should only appear once (or not at all) in the line.

    can it be done?
    how about counting them? just an example.
    Code:
    echo "astrcingb" | awk 'BEGIN{FS=""}{
        for(i=1;i<=NF;i++) r[$i]++
       }
       END{
        for(i in r) {
          if (r[i] ==1 && i == "a" ) a=1
          if (r[i] ==1 && i == "b" ) b=1
          if (r[i] ==1 && i == "c" ) c=1           
        }
        if (a==1 && b==1 && c ==1) print "abc"
       }'

  4. #4
    Join Date
    Oct 2005
    Location
    Naugatuck CT
    Beans
    89
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: regex

    Quote Originally Posted by finer recliner View Post
    is there a way in grep to match individual characters just once?

    for example, i'd like to grep for the letters a, b and c in any order, but each letter should only appear once (or not at all) in the line.

    can it be done?
    Well, the short answer is probably "no." It would be useful to know the goal in building this match.

    It's trivial to match a single character, just use

    Code:
    a{1}
    and you'll match the single character.

    You have two challenges that I see. You only want the single characters and you want to match in any order. But is it your intention to extract the characters, or do you only want a boolean answer (yes they are there, no they aren't there)? And is it true only if all three are there?

    These questions are why it is hard to answer without knowing the purpose of the exercise.

    Also, for all questions regex, the indispensable tool is the regex coach. It's free as in free speech as well as in free beer, and provides you with an interactive GUI for playing with your regex.

    Thanks.

    mp
    If you have to ask what jazz is, you'll never know. -- Louis Armstrong

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •