regular expression question: any character except a white space?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lawrence

    regular expression question: any character except a white space?

    I'm bad at regular expressions. Is this how I would look for any set
    of characters that go more than 60 characters without a white space?

    ..{60}[^ ]



    Also, does this match a block of PHP in an HTML page:



    \<\?PHP .* \?\>
  • Paul Wellner Bou

    #2
    Re: regular expression question: any character except a white space?

    lawrence wrote:[color=blue]
    > I'm bad at regular expressions. Is this how I would look for any set
    > of characters that go more than 60 characters without a white space?
    >
    > .{60}[^ ][/color]

    No, almost ;-)
    take this:

    [^ ]{60,}
    [color=blue]
    > Also, does this match a block of PHP in an HTML page:[/color]

    If there's no white space between, yes.

    Are you looking for wordwrap()?
    --> www.php.net/wordwrap

    Saludo
    Paul.

    Comment

    • CC Zona

      #3
      Re: regular expression question: any character except a white space?

      In article <c1qnqh$1mejqf$ 1@ID-205474.news.uni-berlin.de>,
      Paul Wellner Bou <paul.wellner@u nited-scripts.com> wrote:
      [color=blue]
      > lawrence wrote:[color=green]
      > > I'm bad at regular expressions. Is this how I would look for any set
      > > of characters that go more than 60 characters without a white space?
      > >
      > > .{60}[^ ][/color]
      >
      > No, almost ;-)
      > take this:
      >
      > [^ ]{60,}[/color]

      That would catch 60 character strings, including those containing
      whitespace characters such as newlines, tabs etc. Using preg_match or
      preg_match_all:

      /\S{61,}/

      --
      CC

      Comment

      • Paul Wellner Bou

        #4
        Re: regular expression question: any character except a white space?

        CC Zona wrote:[color=blue][color=green]
        >>[^ ]{60,}[/color]
        > That would catch 60 character strings, including those containing
        > whitespace characters...[/color]

        Excepting the white space character, and that was what he was asking
        for. Ok, perhaps he wanted to include other white space characters,
        too ;-)
        [color=blue]
        > such as newlines,...[/color]

        Only using modifiers /sm.
        [color=blue]
        > tabs etc. Using preg_match or
        > preg_match_all:
        >
        > /\S{61,}/[/color]

        Saludo
        Paul.

        Comment

        • CC Zona

          #5
          Re: regular expression question: any character except a white space?

          In article <c1rifm$1lm948$ 1@ID-205474.news.uni-berlin.de>,
          Paul Wellner Bou <paul.wellner@u nited-scripts.com> wrote:
          [color=blue]
          > CC Zona wrote:[color=green][color=darkred]
          > >>[^ ]{60,}[/color]
          > > That would catch 60 character strings, including those containing
          > > whitespace characters...[/color]
          >
          > Excepting the white space character, and that was what he was asking
          > for.[/color]

          In article <da7e68e8.04022 81025.77340781@ posting.google. com>,
          lkrubner@geocit ies.com (lawrence) wrote:
          [color=blue]
          > I'm bad at regular expressions. Is this how I would look for any set
          > of characters that go more than 60 characters without a white space?[/color]

          "White space". Not "space character".
          [color=blue]
          > Ok, perhaps he wanted to include other white space characters,
          > too ;-)[/color]
          [color=blue][color=green]
          > > such as newlines,...[/color]
          >
          > Only using modifiers /sm.[/color]

          Only if the poster chooses PCRE over ereg, which is unlikely for a regex
          novice.

          $alsowhitespace =array("\n","\r ", "\t", "\v");
          foreach ($alsowhitespac e as $char)
          {
          $str="012345678 90" . $char .
          "12345678901234 567890123456789 012345678901234 56789";
          if (ereg("[^ ]{60,}",$str)) print "Wrong pattern";
          }

          --
          CC

          Comment

          • CC Zona

            #6
            Re: regular expression question: any character except a white space?

            In article <cczona-8CDE5F.23253928 022004@netnews. comcast.net>,
            CC Zona <cczona@nospam. invalid> wrote:
            [color=blue][color=green]
            > > Ok, perhaps he wanted to include other white space characters,
            > > too ;-)[/color]
            >[color=green][color=darkred]
            > > > such as newlines,...[/color]
            > >
            > > Only using modifiers /sm.[/color]
            >
            > Only if the poster chooses PCRE over ereg, which is unlikely for a regex
            > novice.[/color]

            (And, only if either of the "s" or "m" modifier applied to the pattern in
            question, which neither does. The class [^ ] matches newlines et al in
            PCRE as well as ereg.)

            --
            CC

            Comment

            • Paul Wellner Bou

              #7
              Re: regular expression question: any character except a white space?

              >>Only using modifiers /sm.[color=blue]
              >
              > Only if the poster chooses PCRE over ereg, which is unlikely for a regex
              > novice.[/color]

              Why that?
              (I never worked with ereg... and at php.net they recommend using the
              preg functions)

              Comment

              • John Dunlop

                #8
                Re: regular expression question: any character except a white space?

                lawrence wrote:
                [color=blue]
                > Also, does this match a block of PHP in an HTML page:
                >
                > \<\?PHP .* \?\>[/color]

                Did you try it? With no modifiers? All that matches is the string
                "<?PHP" followed by a space followed by zero or more of any character
                (except newlines, by default), followed by a space, followed by the
                string "?>".

                The answer to your question, then, is: possibly, depending on the
                block's tags, its content and the rest of the file. It matches
                almost none of the example blocks given in this newsgroup, for
                example.

                begin example

                <script language=php>
                $foo = '<?PHP '; $bar = ' ?>'
                </script>



                --
                Jock

                Comment

                • lawrence

                  #9
                  Re: regular expression question: any character except a white space?

                  Paul Wellner Bou <paul.wellner@u nited-scripts.com> wrote in message news:<c1qnqh$1m ejqf$1@ID-205474.news.uni-berlin.de>...[color=blue]
                  > lawrence wrote:[color=green]
                  > > I'm bad at regular expressions. Is this how I would look for any set
                  > > of characters that go more than 60 characters without a white space?
                  > >
                  > > .{60}[^ ][/color]
                  >
                  > No, almost ;-)
                  > take this:
                  >
                  > [^ ]{60,}[/color]

                  Thanks much for the response. I've promised myself I'm going to master
                  regex this winter, but I've still a long way to go.



                  [color=blue]
                  > Are you looking for wordwrap()?
                  > --> www.php.net/wordwrap[/color]

                  As discussed in another thread I posted, I dislike wordwrap on the web
                  because in most situations I'm combining it with nl2br and it can lead
                  to jagged line edges. I don't remember why I couldn't put the wordwrap
                  after the nl2br, but for some reason I couldn't.

                  Comment

                  • lawrence

                    #10
                    Re: regular expression question: any character except a white space?

                    ng4rrjanbiah@re diffmail.com (R. Rajesh Jeba Anbiah) wrote in message news:<abc4d8b8. 0402292050.4095 19a2@posting.go ogle.com>...[color=blue]
                    > Try & learn with <http://www.weitz.de/regex-coach/>[/color]

                    Thanks. That was a great suggestion.

                    Comment

                    • Terence

                      #11
                      Re: regular expression question: any character except a white space?

                      I've been using PHP for years now and I still can't do regexs.

                      Everytime I go to use them, it's like once in a blue moon, so I learn
                      just enough for that particular requirement, then all the knowledge
                      leaks out of my head again.

                      and then next time, it's back to square one.

                      I bought the oreiley book "mastering regular expressions" but I never
                      have the time to finish it -- and everytime I come back to it, I have to
                      start again :/

                      Everytime I try reading one of the many condensed guides, I go to use it
                      on my real-life problem and then something is missing. Something is
                      always missing. It's always a vital detail and I never know what it is
                      and I never know where to start looking to find the answer.

                      I don't know how many winters I promised myself I would sit down and
                      learn it properly. Now that I use XML/XSLT for 100% of my websites, I
                      have little need for crunching strings.

                      grrrrrrr

                      (well that post was completely useless wasn't it).

                      Comment

                      • John Dunlop

                        #12
                        Re: regular expression question: any character except a white space?

                        Terence wrote:
                        [color=blue]
                        > I bought the oreiley book "mastering regular expressions" but I never
                        > have the time to finish it -- and everytime I come back to it, I have to
                        > start again :/[/color]

                        What do you think of what you've read so far?
                        [color=blue]
                        > (well that post was completely useless wasn't it).[/color]

                        I don't think so. But you're welcome to your opinion. :-)

                        --
                        Jock

                        Comment

                        • Terence

                          #13
                          Re: regular expression question: any character except a white space?

                          John Dunlop wrote:
                          [color=blue]
                          > Terence wrote:
                          >
                          >[color=green]
                          >>I bought the oreiley book "mastering regular expressions" but I never
                          >>have the time to finish it -- and everytime I come back to it, I have to
                          >>start again :/[/color]
                          >
                          >
                          > What do you think of what you've read so far?
                          >[/color]

                          I can't remember right now, it's been a few months since I started
                          reading it. I'll start reading it again and let you know...

                          Comment

                          Working...