Connecting Tech Pros Worldwide Help | Site Map

Regural expression strange behaviour

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 10:48 AM
Josip
Guest
 
Posts: n/a
Default Regural expression strange behaviour

I have the following code (I need to extract everything between
slash):

$buff="0,x,R / 1,y,R / - / 1,y,R";

preg_match_all("/\s*([0-9]\s*,\s*[a-z]\s*,\s*.\s*)\s*[\/]\s*|\s*(-)\s*[\/]/U",$buff,$izlaz);

var_dump($izlaz);


This outputs:

array(3) {
[0]=>
array(4) {
[0]=>
string(7) "0,x,R /"
[1]=>
string(8) " 1,y,R /"
[2]=>
string(4) " - /"
[3]=>
string(8) " 1,y,R"
}

[1]=>
array(4) {
[0]=>
string(5) "0,x,R"
[1]=>
string(5) "1,y,R"
[2]=>
string(0) ""
[3]=>
string(5) "1,y,R"
}

[2]=>
array(4) {
[0]=>
string(0) ""
[1]=>
string(0) ""
[2]=>
string(1) "-"
[3]=>
string(0) ""
}
}



Problem is in the 3rd row: why is it there? I would expect '-' sign in
2nd row...



  #2  
Old July 17th, 2005, 10:48 AM
Josip
Guest
 
Posts: n/a
Default Re: Regural expression strange behaviour

On Tue, 18 Jan 2005 22:20:09 +0100, Josip <njuzovi@gmail.com> wrote:
[color=blue]
>I have the following code (I need to extract everything between
>slash):
>
>$buff="0,x,R / 1,y,R / - / 1,y,R";
>
>preg_match_all("/\s*([0-9]\s*,\s*[a-z]\s*,\s*.\s*)\s*[\/]\s*|\s*(-)\s*[\/]/U",$buff,$izlaz);
>[/color]

Hm, this is partial solution...

preg_match_all("/(\s*[0-9]\s*,\s*[a-z]\s*,\s*[LR]\s*\s*|\s*[\-]\s*)[\/]\s*/U",$buff,$izlaz);

  #3  
Old July 17th, 2005, 10:48 AM
John Dunlop
Guest
 
Posts: n/a
Default Re: Regural expression strange behaviour

Josip wrote:
[color=blue]
> I have the following code (I need to extract everything between
> slash):
>
> $buff="0,x,R / 1,y,R / - / 1,y,R";
>
> preg_match_all("/\s*([0-9]\s*,\s*[a-z]\s*,\s*.\s*)\s*[\/]\s*|\s*(-)\s*[\/]/U",$buff,$izlaz);[/color]

Is that really your pattern? I only ask because the output
below doesn't come from that.
[color=blue]
> var_dump($izlaz);
>
>
> This outputs:
>
> array(3) {
> [0]=>
> array(4) {[/color]

No it doesn't. There are only three matches to the full
pattern in your subject string.
[color=blue]
> [0]=>
> string(7) "0,x,R /"
> [1]=>
> string(8) " 1,y,R /"
> [2]=>
> string(4) " - /"
> [3]=>
> string(8) " 1,y,R"[/color]

The last string here doesn't match because there must be a
slash at the end. You should allow the last slash to be
optional in your pattern. Consider:

`(\d\s*,\s*[a-z]\s*,\s*.|-)\s*/?`

[ ... ]
[color=blue]
> Problem is in the 3rd row: why is it there? I would expect '-' sign in
> 2nd row...[/color]

Your subject string is '0,x,R / 1,y,R / - / 1,y,R'. The
hyphen is in the third segment, not the second. Move the
hyphen to the second segment and watch what happens.

--
Jock
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.