I want to match words which have special characters in it...

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

I want to match words which have special characters in it...

Post by raghavan20 »

I expect to match
rough**ly
tha&&t
docum//ents

Code: Select all

<?php

$input = <<<EOD
Character data is rough**ly all the (non-markup) contents2343 of XML docum//ents, including white234324space 

between tags. Note tha&&t the XML par2756724ser does not add or remove any (whitespace), it is up to the33333 

application (you) to decide whether whitespace5555 is significant. 
EOD;

echo preg_match_all("/\b\w+(\W+)\w+\b/mi", $input, $matches)."<br />";
print_r($matches);

?>
but this returns...

Code: Select all

Array
(
    [0] => Array
        (
            [0] => Character data
            [1] => is rough
            [2] => ly all
            [3] => the (non
            [4] => markup) contents2343
            [5] => of XML
            [6] => docum//ents
            [7] => including white234324space
            [8] => between tags
            [9] => Note tha
            [10] => t the
            [11] => XML par2756724ser
            [12] => does not
            [13] => add or
            [14] => remove any
            [15] => whitespace), it
            [16] => is up
            [17] => to the33333
            [18] => application (you
            [19] => to decide
            [20] => whether whitespace5555
            [21] => is significant
        )

    [1] => Array
        (
            [0] =>  
            [1] =>  
            [2] =>  
            [3] =>  (
            [4] => ) 
            [5] =>  
            [6] => //
            [7] =>  
            [8] =>  
            [9] =>  
            [10] =>  
            [11] =>  
            [12] =>  
            [13] =>  
            [14] =>  
            [15] => ), 
            [16] =>  
            [17] =>  
            [18] =>  (
            [19] =>  
            [20] =>  
            [21] =>  
        )
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

#\b[A-z]*[\*/&]+[A-z]+#
possibly... can't test it right now..
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

but I want to check for any special character not specifically for *,/ and & characters...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

:roll: try this then..

Code: Select all

#\b[A-z]*[^\sA-z0-9]+[A-z]+[^\sA-z0-9]*#
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

This is the output for your regex

Code: Select all

Array
(
    [0] => Array
        (
            [0] => rough**ly
            [1] => non-markup)
            [2] => docum//ents,
            [3] => tha&&t
        )

)
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

This worked...#\b[a-z]+([^a-z0-9\s]+)[a-z]+\b#i

Code: Select all

Array
(
    [0] => Array
        (
            [0] => rough**ly
            [1] => non-markup
            [2] => docum//ents
            [3] => tha&&t
        )

    [1] => Array
        (
            [0] => **
            [1] => -
            [2] => //
            [3] => &&
        )

)
Post Reply