Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

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.
0
DrAske
Asked:
DrAske
  • 3
  • 2
1 Solution
 
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
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ddrudikCommented:
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
 
DrAskeAuthor Commented:
thank you.
0
 
ddrudikCommented:
Thanks for the question and the points.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now