Professional Web Applications Themes

regexp problem - PHP Development

First, I need the code to replace the link in a string, like <BASE HREF="www.google.com"> with <BASE HREF="mystring"> ereg_replace("'<BASE HREF=\"'(.*)'\">'", "'<BASE HREF=\"$my_string\">'", $old); That was the closest i got, but (.*) makes it take the first BASE HREF, and the last ">, which s. Can anyone help? Second, I hate asking for help on Regexps...I would like a tutorial online that steps me through one method of them at a time, and then lets me move on...kindof like math. You have to know the basics befor you can get advanced. Any help there either?...

  1. #1

    Default regexp problem

    First,
    I need the code to replace the link in a string, like

    <BASE HREF="www.google.com"> with <BASE HREF="mystring">

    ereg_replace("'<BASE HREF=\"'(.*)'\">'", "'<BASE HREF=\"$my_string\">'",
    $old);

    That was the closest i got, but (.*) makes it take the first BASE HREF, and
    the last ">, which s.
    Can anyone help?

    Second,
    I hate asking for help on Regexps...I would like a tutorial online that
    steps me through one method of them at a time, and then lets me move
    on...kindof like math. You have to know the basics befor you can get
    advanced. Any help there either?


    Taylor York Guest

  2. #2

    Default Re: [PHP] regexp problem

    On Thursday, July 10, 2003, 9:25:31 PM, Taylor wrote:

    TY> First,
    TY> I need the code to replace the link in a string, like

    TY> <BASE HREF="www.google.com"> with <BASE HREF="mystring">

    TY> ereg_replace("'<BASE HREF=\"'(.*)'\">'", "'<BASE HREF=\"$my_string\">'",
    TY> $old);

    TY> That was the closest i got, but (.*) makes it take the first BASE HREF, and
    the last ">>, which s.
    TY> Can anyone help?

    I'm not a regex guru by any means, but I think your problem here is
    that your expression is "greedy". Try (.?) ... I think.

    TY> Second,
    TY> I hate asking for help on Regexps...I would like a tutorial online that
    TY> steps me through one method of them at a time, and then lets me move
    TY> on...kindof like math. You have to know the basics befor you can get
    TY> advanced. Any help there either?

    I have found that the best people to ask for regex questions are PERL
    programmers. They seem to have the insider knowledge on this stuff.

    There are a few online tutorials that I've seen, but none that I could
    recommend with any confidence. They are simply "this expression does
    this, and put it together ... wow it works! Regexs are great!"

    There is a #perl channel on freenode (IRC) that I have found to be
    extremely helpful in this regard.

    If you do find any good tutorials, I would appreciate an offline.


    --
    Regards,
    Burhan Khalid
    phplist[at]meidomus[dot]com
    [url]http://www.meidomus.com[/url]

    Burhan Khalid Guest

  3. #3

    Default regexp problem

    Hello all,
    Is there any way in regular expression how to specify
    that searched text must not include given word.
    Something like [^chars] for words.
    Thanks in advance,
    Jaroslav


    Jaroslav Jakl Guest

  4. #4

    Default Re: regexp problem

    On Fri, 25 Jul 2003 16:19:38 +0200, "Jaroslav Jakl" <j.jaklera.cz> wrote:
    >Is there any way in regular expression how to specify
    >that searched text must not include given word.
    >Something like [^chars] for words.
    If you're using the preg_* functions, then a zero-width negative look-ahead
    assertion such as:

    (?!badword)

    ... may be what you want.

    Or just reverse the logic, i.e. 'if ( ! preg_match($bad_pattern, $subject) )'

    --
    Andy Hassall (andyandyh.co.uk) icq(5747695) ([url]http://www.andyh.co.uk[/url])
    Space: disk usage ysis tool ([url]http://www.andyhsoftware.co.uk/space[/url])
    Andy Hassall Guest

  5. #5

    Default Regexp problem



    Suppose I have some lengthy and/or complicated sub-regexp such as
    '(foo|bar|...)', how do I write a regexp that will match it, followed
    by some white space, followed by something that is neither foo or
    bar? I.e. I want this to match:

    bar frobozz

    but not this:

    bar foo

    TIA,

    Jill

    J Krugman Guest

  6. #6

    Default Re: Regexp problem

    On Mon, 11 Aug 2003 01:08:37 +0000 (UTC),
    J Krugman <jill_krugman> wrote:
    >
    >
    > Suppose I have some lengthy and/or complicated sub-regexp such as
    > '(foo|bar|...)', how do I write a regexp that will match it, followed
    > by some white space, followed by something that is neither foo or
    > bar? I.e. I want this to match:
    By translating your english into perl regular expression syntax:

    match it: (foo|bar|...)

    white space: \s+

    something: \S

    neither foo or bar : (?!foo|bar)

    The neither bit if the only not so common construct, see "perldoc perlre"
    for details.


    And putting it all together:

    /(foo|bar|...)\s+(?!foo|bar)\S/s


    I made a bunch of assumptions about what your words meant, things like
    "some" being 1 or more, "something" being 1 or more non-whitespace
    characters. If they are wrong, then the regex will be wrong.

    I also assumed you didn't want:

    "foo barn"

    to match, but did want:

    "foo seafood"

    to match. Again, if that's wrong the regex is wrong (but easily fixed).

    --
    Sam Holden

    Sam Holden Guest

  7. #7

    Default Re: Regexp problem



    J Krugman wrote:
    >
    > Suppose I have some lengthy and/or complicated sub-regexp such as
    > '(foo|bar|...)', how do I write a regexp that will match it, followed
    > by some white space, followed by something that is neither foo or
    > bar? I.e. I want this to match:
    >
    > bar frobozz
    >
    > but not this:
    >
    > bar foo
    my $first_part = qr/foo|bar|.../;

    my $regex = qr/$first_part \s+ (?!$first_part) \S/x;

    I'm not entirely sure how perl optomizes this, so you might want to
    prevent perl from backtracking over the \s+, as follows:

    my $regex = qr/$first_part (?>\s+) (?!$first_part) \S/x;

    This should only have a significant effect if you've got a string such
    as "bar foo" (with lots of whitespace in between the two
    parts). Without the (?>) it might take an excessive time to fail.

    --
    $a=24;split//,240513;s/\B/ => /for=qw(ac ab bc ba cb ca
    );{push(b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$[$a%6
    ]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop b))&&redo;}
    Benjamin Goldberg Guest

  8. #8

    Default regexp problem

    I'm trying to detect the string "0,0,0" ( that's numeric comma numeric
    comma numeric) which is a value returned by cdrecord -scanbus
    for detecting CD-ROM drives attached to a Linux system.
    I have tried numerous regexp expressions but cannot get that string to
    be detected.

    eg : "[0-9]\.[0-9]\,[0-9]"

    I assume that the comma is causing the trouble but even using \ escape
    ( \,) does not solve the problem.

    Help please, thanks


    Brett S Hallett Guest

  9. #9

    Default Re: regexp problem

    Brett S Hallett wrote:
    > I'm trying to detect the string "0,0,0" ( that's numeric comma numeric
    > comma numeric) which is a value returned by cdrecord -scanbus
    > for detecting CD-ROM drives attached to a Linux system.
    > I have tried numerous regexp expressions but cannot get that string to
    > be detected.
    >
    > eg : "[0-9]\.[0-9]\,[0-9]"
    >
    > I assume that the comma is causing the trouble but even using \ escape (
    > \,) does not solve the problem.
    >
    > Help please, thanks
    Your first "comma" there is a period, which would be ok except you've
    escaped it to mean a literal "." instead of its meta-char meaning of
    "any character".

    /[0-9],[0-9],[0-9]/.match("0,1,2")

    works for me.



    Michael campbell Guest

  10. #10

    Default Re: regexp problem

    Brett S Hallett <dragoncityimpulse.net.au> writes:
    > I'm trying to detect the string "0,0,0" ( that's numeric comma numeric
    > comma numeric) which is a value returned by cdrecord -scanbus
    > for detecting CD-ROM drives attached to a Linux system.
    > I have tried numerous regexp expressions but cannot get that string to
    > be detected.
    >
    > eg : "[0-9]\.[0-9]\,[0-9]"
    >
    > I assume that the comma is causing the trouble but even using \ escape
    > ( \,) does not solve the problem.
    No, it's not the comma.

    $ ruby -e 'puts /[0-9],[0-9],[0-9]/.match("0,0,0")'
    0,0,0

    Tim
    Tim Heaney Guest

  11. #11

    Default Re: regexp problem

    Hi,

    From: "Michael campbell" <michael_s_campbell>
    > Brett S Hallett wrote:
    > >
    > > eg : "[0-9]\.[0-9]\,[0-9]"
    >
    > /[0-9],[0-9],[0-9]/.match("0,1,2")
    Also, /\d,\d,\d/


    Regards,

    Bill



    Bill Kelly Guest

  12. #12

    Default Re: regexp problem

    If your 'numerics' can be numbers larger then 0-9 then I'm going to add to
    what Bill said:

    /\d+,\d+,\d+/

    This should allows one or more digits followed by a comma, followed by one
    or more digits,
    followed by a comma, followed by one more or digits.

    HTH

    -Zach

    -----Original Message-----
    From: Bill Kelly [mailto:billkcts.com]
    Sent: Thursday, October 16, 2003 9:04 PM
    To: ruby-talk ML
    Subject: Re: regexp problem


    Hi,

    From: "Michael campbell" <michael_s_campbell>
    > Brett S Hallett wrote:
    > >
    > > eg : "[0-9]\.[0-9]\,[0-9]"
    >
    > /[0-9],[0-9],[0-9]/.match("0,1,2")
    Also, /\d,\d,\d/


    Regards,

    Bill





    Zach Dennis Guest

  13. #13

    Default Re: regexp problem


    "Bill Kelly" <billkcts.com> schrieb im Newsbeitrag
    news:02ba01c3944a$dc497c00$6404a8c0musicbox...
    > Hi,
    >
    > From: "Michael campbell" <michael_s_campbell>
    > > Brett S Hallett wrote:
    > > >
    > > > eg : "[0-9]\.[0-9]\,[0-9]"
    > >
    > > /[0-9],[0-9],[0-9]/.match("0,1,2")
    >
    > Also, /\d,\d,\d/
    and /\d(,\d){2}/
    or for full integer match support /[+-]?\d+(,[+-]?\d+){2}/

    robert

    Robert Klemme Guest

  14. #14

    Default Re: regexp problem

    In article <bmo8ee$p2juc$2ID-52924.news.uni-berlin.de>,
    Robert Klemme <bob.newsgmx.net> wrote:
    >or for full integer match support /[+-]?\d+(,[+-]?\d+){2}/
    Once I start repeating chunks of regular expressions I try to remember
    to name them e.g.
    >> int = /[+-]?\d+/
    => /[+-]?\d+/
    >> /#{int}(?:,#{int}){2}/.match("0,1,2")
    => #<MatchData:0x401ffaf8>
    >> /#{int}(?:,#{int}){2}/.match("0,1,")
    => nil

    Mike
    --
    [email]mikestok.co.uk[/email] | The "`Stok' disclaimers" apply.
    [url]http://www.stok.co.uk/~mike/[/url] | GPG PGP Key 1024D/059913DA
    [email]mikeexegenix.com[/email] | Fingerprint 0570 71CD 6790 7C28 3D60
    [url]http://www.exegenix.com/[/url] | 75D2 9EC4 C1C0 0599 13DA
    Mike Stok Guest

  15. #15

    Default Re: regexp problem


    "Mike Stok" <mikeratdog.stok.co.uk> schrieb im Newsbeitrag
    news:fXPjb.361021$Lnr1.131244news01.bloor.is.net. cable.rogers.com...
    > In article <bmo8ee$p2juc$2ID-52924.news.uni-berlin.de>,
    > Robert Klemme <bob.newsgmx.net> wrote:
    >
    > >or for full integer match support /[+-]?\d+(,[+-]?\d+){2}/
    >
    > Once I start repeating chunks of regular expressions I try to remember
    > to name them e.g.

    Reasonable.
    > >> int = /[+-]?\d+/
    > => /[+-]?\d+/
    > >> /#{int}(?:,#{int}){2}/.match("0,1,2")
    > => #<MatchData:0x401ffaf8>
    > >> /#{int}(?:,#{int}){2}/.match("0,1,")
    > => nil
    This leads to interesting points:

    Why not:

    irb(main):010:0> int='[+-]?\d+'
    => "[+-]?\\d+"
    irb(main):011:0> /#{int}(?:,#{int}){2}/o.match("0,1,2").to_a
    => ["0,1,2"]
    irb(main):012:0> /#{int}(?:,#{int}){2}/o.match("0,1,")
    => nil
    irb(main):013:0>

    The difference being:

    irb(main):021:0> int = /[+-]?\d+/
    => /[+-]?\d+/
    irb(main):022:0> /#{int}(?:,#{int}){2}/
    => /(?-mix:[+-]?\d+)(?:,(?-mix:[+-]?\d+)){2}/
    irb(main):023:0> int = '[+-]?\d+'
    => "[+-]?\\d+"
    irb(main):024:0> /#{int}(?:,#{int}){2}/
    => /[+-]?\d+(?:,[+-]?\d+){2}/
    irb(main):025:0>

    i.e. why did you make 'int' a regular expression? And: one should use "o"
    flag in case you break up the rx only for readability / typing effort.

    Regards

    robert

    Robert Klemme Guest

Similar Threads

  1. regexp
    By Ramprasad A Padmanabhan in forum PERL Beginners
    Replies: 5
    Last Post: May 28th, 12:56 PM
  2. regexp help
    By Janek Schleicher in forum PERL Miscellaneous
    Replies: 22
    Last Post: October 1st, 02:25 PM
  3. regexp help please!
    By Justin French in forum PHP Development
    Replies: 0
    Last Post: September 22nd, 01:20 AM
  4. i need a regexp
    By Felix Stegerman in forum PHP Development
    Replies: 3
    Last Post: August 31st, 06:14 PM
  5. regexp problem with UTF8
    By Risto Vaarandi in forum PERL Miscellaneous
    Replies: 0
    Last Post: July 16th, 03:03 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139