We help IT Professionals succeed at work.

Regular Expression - URGENT

288 Views
Last Modified: 2013-11-07
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.
Comment
Watch Question

CERTIFIED EXPERT

Commented:
<?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']
        )

)

Author

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,

Commented:
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.
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
thank you.
CERTIFIED EXPERT

Commented:
Thanks for the question and the points.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.