SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: grep

  1. #1
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    grep

    Code:
    {TAG}
    0012310002 and 0012310003
    {COUNT}
    000000
    {COUNT2}
    000000 and 100001
    how to write a regular expression, to find the text between closing of one '}' to start of another '{'. And also check if 'and' is present, if 'and' is present, then I have to print "{TAG} and {COUNT2} is 1".

    or please tell me how to store the between {TAG} to start of another braces.

  2. #2
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    grep

    Code:
    {TAG}
    0012310002 and 0012310003
    {COUNT}
    000000
    {COUNT2}
    000000 and 100001
    How to store the value after {TAG} to start of '{' of COUNT.
    i.e,
    0012310002 and 0012310003 store the values to a variable

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how to write a regular expression, to find the text between closing of one '}' to start of another '{'.
    Who needs a regular expression?

    Code Python:
    s = """{TAG}
    0012310002 and 0012310003
    {COUNT}
    000000
    {COUNT2}
    000000 and 100001
    """
     
    lines = s.split("\n")
    results = []
     
    for line in lines:
        if 'and' in line:
            results.append(line)
     
     
    print results
     
    --output:--
    ['0012310002 and 0012310003', '000000 and 100001']


    or please tell me how to store the between {TAG} to start of another braces.
    Code Python:
    import re
     
    s = """{TAG}
    0012310002 and 0012310003
    {COUNT}
    000000
    {COUNT2}
    000000 and 100001
    """
     
    pattern = "{TAG}\n(.+?)\n{"
    regex = re.compile(pattern, re.DOTALL)
     
    print regex.findall(s)
     
    --output:--
    ['0012310002 and 0012310003']

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    What about...
    PHP Code:
    <?php
    $sString 
    '
    {TAG}
    0012310002 and 0012310003
    {COUNT}
    000000
    {COUNT2}
    000000 and 100001
    '
    ;
    if(
    === preg_match('~(?<=\{TAG\})[^\{]+~'$sString$aMatches))
    {
        echo 
    array_shift($aMatches); #0012310002 and 0012310003 
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    $var "{TAG}
    0012310002 and 0012310003
    {COUNT}
    000000
    {COUNT2}
    000000 and 100001"
    ;

    $xexplodePHP_EOL$var ) ; // amazed this worked first time

    var_dump$x[1] ); 
    This only works in the case that the second line is your "needle" in the haystack.

    You could just as easily loop through $x till you get to {tag} then display the next one, or then again use a regular expression to isolate the variable you want.

    Give more examples if your last one was not typical.

    Edit:

    Bah, SBUK is having another easy day at work again I see ...

  6. #6
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    What about...
    Don't forget, that will include newlines surrounding the "0012310002 and 0012310003" value. The OP wasn't clear but I'd guess they don't want to include those in the value. That said, it's pretty trivial to remove them or not capture them in the first place.
    Salathe
    Software Developer and PHP Manual Author.

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:


    Blasphemy! Pretty hard at it actually.

    I've managed to hire our favourite Scot (MMcG) for some internal design work here. Which just leaves the server, platform, code and logic to me, yay.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  8. #8
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply.

    Autually
    it is large file and cannot store as a string. So can you please tell me how can I do it reading from the file,

    {TAG}
    somevalue
    {COUNT}

    if the somevalue contains "and", then I have print the {TAG} is multiple

  9. #9
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Edit:

    Whoops. I could have sworn I was in PP&0 when I replied.

  10. #10
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Why don't you post an example of what you have before, and an example of what the exact output you desire after the operation.

    Think about it carefully and make the example(s) as close to real life, worst cases scenarios, as you can.

    Wrap the before and after examples in these tags : [ code ] ... [ /code ] <- without the spaces, and they will be formatted nicely.

    You can read a file into a variable e.g. $var


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •