Thread: What is regular expression for a word

  1. #1
    Registered User Hannibal2010's Avatar
    Join Date
    Jun 2011
    Location
    Hanoi, Vietnam
    Posts
    46

    What is regular expression for a word

    Dear all,

    Now I need a regular expression to describe a specific word.

    This word is followed by tab, space, or end of line character.

    So I tried it:

    Code:
    my_word[\r\n[^ tab]\+]\*
    Now, it can recognize a word follow by a space/tab, but it fails if this word is end of line (it means, it followed by end of line: \r or \n)

    Please help me.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you could phrase your question in the form of a joke, you might have better luck getting a response.

    See also: Regular Expressions Reference - Basic Syntax ... I think you want \w


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    $ matches the end of a line, but you can't use it in a bracketed character class (that is, inside of []), so:
    Code:
    my_word([ \t]|$)
    That matches my_word, followed by either a space or a tab, or the end of the string. You might instead want:
    Code:
    my_word[ \t\r\n]
    This matches my_word followed by either space, tab, \r, or \n.

    It all depends on how your regular expression engine matches the end of a line. Often, \n is treated as the end of a line, and $ will thus match it; but this can be changed in some (most?) engines.

  4. #4
    Registered User Hannibal2010's Avatar
    Join Date
    Jun 2011
    Location
    Hanoi, Vietnam
    Posts
    46
    Dear cas,

    Thanks for your help.

    My way is similar with yours, but it can not recognize the end of string (another cases are okie)

    For example:
    my_word\n --> cannot find

  5. #5
    Registered User Hannibal2010's Avatar
    Join Date
    Jun 2011
    Location
    Hanoi, Vietnam
    Posts
    46
    I used \b after my_word, it seems be OK

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    \b matches any word boundary (which means anything but [a-zA-Z0-9_]); that's not what you originally asked for. If it works, great, but it's different.
    My way is similar with yours, but it can not recognize the end of string (another cases are okie)
    I showed you two ways. Both of which work, or at least can work. In the future, I'd recommend giving more information when you're having trouble with something. For example, you could say that you tested both of the versions that I showed you and that neither worked. In addition, you could mention what regular expression library you're using. As things stand, you gave very little information.

  7. #7
    Registered User Hannibal2010's Avatar
    Join Date
    Jun 2011
    Location
    Hanoi, Vietnam
    Posts
    46
    Dear cas,

    Maybe my English skill is not good enough, but however, thank you very much for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. regular expression
    By ashaikh432 in forum C++ Programming
    Replies: 2
    Last Post: 09-24-2010, 06:40 AM
  2. Simple regular expression to find word in a string?
    By BC2210 in forum C Programming
    Replies: 1
    Last Post: 03-28-2010, 07:41 PM
  3. Regular Expression
    By csonx_p in forum Tech Board
    Replies: 8
    Last Post: 09-03-2008, 09:10 AM
  4. Regular Expression
    By stevesmithx in forum C Programming
    Replies: 0
    Last Post: 02-18-2008, 11:00 AM
  5. Regular Expression..
    By vasanth in forum Tech Board
    Replies: 3
    Last Post: 08-03-2004, 07:56 AM