Thread: First time writing a regular expression

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    228

    First time writing a regular expression

    So I've been reading a little bit on regular expressions and how to make one. After reading a little bit on what each character meant, I decide to make a very simple regular expression to get me started.

    Code:
    #include <stdio.h>
    
    
    int main()
    {
        char str[128] = {0};
    
    
        scanf("%[Nn]*icks", str);
        printf("%s\n", str);
    
    
        return 0;
    }
    the printf function is suppose to print out words that start out with the letter N or n and end with any of the remaining characters.

    So words like Nick, nick, ick, nnick, etc matches the pattern that I left in the scanf function. However, at its current state, printf only prints N or n and nothing more.

    What am I missing?
    Last edited by deathslice; 05-11-2015 at 12:59 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    scanf doesn't do regular expressions.

    The things in square brackets are scan sets.

    So scanf("%[Nn]*icks" basically matches one or more of Nn, followed by a literal "*icks"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    228
    Quote Originally Posted by Salem View Post
    scanf doesn't do regular expressions.
    But isn't regular expression all about pattern matching? So if you can use scan sets to look for patterns, why can't you say that scanf has at least some functionality of how a regular expression work which is to look for patterns in a given string and see if it matches?

    I guess what I'm asking is why the format specifiers in scanf are not generally considered to be a species of regular expressions?

  4. #4
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by deathslice View Post
    I guess what I'm asking is why the format specifiers in scanf are not generally considered to be a species of regular expressions?
    Because they're completely different?

    Regular expressions are different to glob patterns are different to C scanf() conversions are different to printf() format strings.

    You are essentially asking why languages other than English exist.

    Character/string patterns are just a group of very domain-specific languages, and they have quite a bit of history behind them. All the above are decades old, already.

  5. #5
    Registered User
    Join Date
    May 2015
    Posts
    228
    Alright thanks anyways.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a regular expression, please?
    By Dragoon_42 in forum C# Programming
    Replies: 2
    Last Post: 09-20-2012, 11:30 AM
  2. regular expression
    By ashaikh432 in forum C++ Programming
    Replies: 2
    Last Post: 09-24-2010, 06:40 AM
  3. Regular Expression
    By stevesmithx in forum C Programming
    Replies: 0
    Last Post: 02-18-2008, 11:00 AM
  4. about regular expression
    By asert in forum C Programming
    Replies: 1
    Last Post: 11-07-2006, 11:53 AM
  5. Regular Expression
    By tintifaxe in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2006, 07:16 AM

Tags for this Thread