Regular Expression - URGENT

Hi Experts ..

I want a regular exprssion that parse the following string

/package[@name='mypackage']/class[@name='myclass']/method[@name='something' and param[1]/typevaluespec='int']

I want the first regular expression to return:

package[@name='mypackage']
class[@name='myclass']
method[@name='something' and param[1]/typevaluespec='int']

I cannot split the string using '/' character since (3) contain '/' character.
this is URGENT!! please help.
LVL 9
DrAskeAsked:
Who is Participating?
 
ddrudikConnect With a Mentor Commented:
DrAske, failing to provide real-world examples only delays correct answers, not recommended if it's urgent.
BTW, technically I got you right, however you didn't ask the question you wanted an answer for.

$pattern = '@(/[a-z]+.*?)(/[a-z]+.*?)(/[a-z]+.*?$)@';
0
 
ddrudikCommented:
<?php
$string = <<<EOF
/package[@name='mypackage']/class[@name='myclass']/method[@name='something' and param[1]/typevaluespec='int']
EOF;
$pattern = '@(/package.*?)(/class.*?)(/method.*?$)@';
preg_match_all($pattern, $string, $array);
echo '<pre>', print_r($array, true), '</pre>';
?>

PHP Version = 5.2.4
PCRE Library Version = 7.2 2007-06-19

Array
(
    [0] => Array
        (
            [0] => /package[@name='mypackage']/class[@name='myclass']/method[@name='something' and param[1]/typevaluespec='int']
        )

    [1] => Array
        (
            [0] => /package[@name='mypackage']
        )

    [2] => Array
        (
            [0] => /class[@name='myclass']
        )

    [3] => Array
        (
            [0] => /method[@name='something' and param[1]/typevaluespec='int']
        )

)

0
 
DrAskeAuthor Commented:
ooh!! I am sooo sorry, you got me wrong!!

/package[@name='mypackage']/class[@name='myclass']/method[@name='something' and param[1]/typevaluespec='int']

package, class , method are just an example!! you can say that the string between '/' and '[' character
must be [a-z]+.

My apologies,
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
dkloeckCommented:
string testString = "/package[@name='mypackage']/class[@name='myclass']/method" +
                "[@name='something' and param[1]/typevaluespec='int']";

            Regex packageRegex = new Regex("/.[^\\]]+");//

            textBox1.Text = "";
            foreach (Match m in packageRegex.Matches(testString))
                textBox1.Text += m.Value + "]" + " ;$; ";


the regex "/.[^\\]]+" will get you all string between / and ] (it will exclude the ] so you will have to put it to the string after.
0
 
DrAskeAuthor Commented:
thank you.
0
 
ddrudikCommented:
Thanks for the question and the points.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.