Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Regex to remove citations: [1][2][3]

Last post 10-10-2008, 3:39 PM by prometheuzz. 3 replies.
Sort Posts: Previous Next
  •  10-10-2008, 2:15 PM 47084

    Regex to remove citations: [1][2][3]

    I have been bashing my head trying to find a way to do this.

     I am cleaning out some text files and most of the time a simple "Find>Replace" method works but I am trying to remove somthing like this:

      The text file would look like:

    Hello my name is Dave [1], I am from California [2] an I like fishing [3].

     I would like to strip out all of the numbers in brackets (including the brackets themselves) from the number 1 to 100. So the text looks like:

     Hello my name is Dave, I am from California an I like fishing.

    Any help on creating a regex that can do this?

    Thanks

  •  10-10-2008, 2:53 PM 47092 in reply to 47084

    Re: Regex to remove citations: [1][2][3]

    The regex:

    \[([1-9]\d?|100)\]

    will match any number, from 1 to 100 (both inclusive), inside square brackets.

    So, you simply search for that pattern using your regex-tool or programming language (I don't know what you're using) and replace those with an empty string.

    HTH

  •  10-10-2008, 3:24 PM 47096 in reply to 47092

    Re: Regex to remove citations: [1][2][3]

    Thanks for the help. I am staring at it and I still can't decipher it.

    People that are amazed of programmers, well programmers should be amazed of regex experts.

     Thanks!

  •  10-10-2008, 3:39 PM 47097 in reply to 47096

    Re: Regex to remove citations: [1][2][3]

    specmav:

    Thanks for the help. I am staring at it and I still can't decipher it.

    People that are amazed of programmers, well programmers should be amazed of regex experts.

     Thanks!

    You're most welcome.

    Here's an explanation:

    \[         // matches an opening square bracket
    (          // open group 1
      [1-9]    // matches '1', '2', '3', '4', '5', '6', '7', '8' or '9'
      \d?      // _optionally_ matches '1', '2', '3', '4', '5', '6', '7', '8', '9' or '0'
      |        // OR
      100      // matches '100'
    )          // close group 1
    \]


    So in plain English: "Match '[', followed by any number except '0' optionally followed by any number, or, '100', and finally, ending with ']'".

    Note that without the parenthesis, \[[1-9]\d?|100\],

    \[[1-9]\d? // the first part
    |          // OR
    100\]      // the second part
    ... it would match only the first OR the second part. So numbers with a '[' in front of it or with a ']' at the end would match, but not numbers with both '[' and ']'!
     

     

View as RSS news feed in XML