Regular Expression Logic problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karlectomy
    New Member
    • Sep 2007
    • 64

    #1

    Regular Expression Logic problem

    Hey all,

    I have a parsing application where I am taking text files and choppiing them up (so to speak) There are small formatting differences which I was hoping to take care of with regular expression logic.

    Here is example of one line:

    $CHAP_01.txt$ 1 Chapter One

    This line should be equivalent:

    $CHAP_01.txt$ 1. Chapter One

    (See the period after the one?)

    I am using the split function with the regular expression " \\d+ " which works fine for the first one but for the second, I get an out of bounds exception when I refer to index 1 in the output array.

    I was wondering if there was some way to use " \\d+ [ \\d+\\. ]" or some other logic in the regular expressions to split those two line s the same..

    THanks

    Karl
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by karlectomy
    Hey all,

    I have a parsing application where I am taking text files and choppiing them up (so to speak) There are small formatting differences which I was hoping to take care of with regular expression logic.

    Here is example of one line:

    $CHAP_01.txt$ 1 Chapter One

    This line should be equivalent:

    $CHAP_01.txt$ 1. Chapter One

    (See the period after the one?)

    I am using the split function with the regular expression " \\d+ " which works fine for the first one but for the second, I get an out of bounds exception when I refer to index 1 in the output array.

    I was wondering if there was some way to use " \\d+ [ \\d+\\. ]" or some other logic in the regular expressions to split those two line s the same..

    THanks

    Karl
    You want a literal dot at the end zero or more times; regular expressions can
    easily take care of that: \.?

    That is: a literal dot (escaped) with a closure ? (zero or one times). Of course
    you have to use two backslashes it you want to put that regular expression in
    a literal string: \\.?

    kind regards,

    Jos

    Comment

    • karlectomy
      New Member
      • Sep 2007
      • 64

      #3
      Originally posted by JosAH
      You want a literal dot at the end zero or more times; regular expressions can
      easily take care of that: \.?

      That is: a literal dot (escaped) with a closure ? (zero or one times). Of course
      you have to use two backslashes it you want to put that regular expression in
      a literal string: \\.?

      kind regards,

      Jos
      Thanks Jos,

      That works! but I also figured it out on my own using " \\d+(\\. | )" which is a dot followed by a space or just a space.

      THanks for your help,

      Karl

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by karlectomy
        Thanks Jos,

        That works! but I also figured it out on my own using " \\d+(\\. | )" which is a dot followed by a space or just a space.

        THanks for your help,

        Karl
        Yep, you're welcome of course; when you use regular expressions there are more
        ways that lead to Rome ;-)

        kind regards,

        Jos

        Comment

        • karlectomy
          New Member
          • Sep 2007
          • 64

          #5
          Originally posted by JosAH
          Yep, you're welcome of course; when you use regular expressions there are more
          ways that lead to Rome ;-)

          kind regards,

          Jos
          That's very true and also, com.cities.rome wasn't built in a day :)

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by karlectomy
            That's very true and also, com.cities.rome wasn't built in a day :)
            True; and don't forget: when in Rome do as the Romans do.

            kind regards,

            Jos (<--- walks like an Egyptian ;-)

            Comment

            Working...