Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Repetition in Regular Expressions

This irb session example helps demonstrate repetition in regular expressions using * ? + {}

Definitions:

? - the preceding token is optional
+ - find the token 1 or more times
* - find the token 0 or more times
{n} - repeat the token n no of times


irb(main):058:0> "this song is my favourite"[/favou?rite/]
=> "favourite"
irb(main):059:0> "this song is my favorite"[/favou?rite/]
=> "favorite"

irb(main):100:0> "this is my favourite\s"[/this\s((song|video)\s)?is/]
=> "this is"
irb(main):101:0> "this song is my favourite\s"[/this\s((song|video)\s)?is/]
=> "this song is"
irb(main):102:0> "this flower is my favourite\s"[/this\s((song|video)\s)?is/]
=> nil
irb(main):103:0> "this video is my favourite\s"[/this\s((song|video)\s)?is/]
=> "this video is"

irb(main):105:0> "anything you say"[/.*/]
=> "anything you say"
irb(main):119:0> "will be repeated"[/.*/]
=> "will be repeated"
irb(main):116:0> "good times</p>"[/.*[^<\/p>]/]
=> "good times"

irb(main):220:0> "test 123 test 123"[/\d+/]
=> "123"
irb(main):224:0> "test 123 test 123"[/\d{2}/]
=> "12"
irb(main):226:0> "test 123 test 123"[/\w+/]
=> "test"

irb(main):008:0> "try this test that"[/try.\b\w+/]
=> "try this"
irb(main):009:0> "try this test that"[/test.\b\w+/]
=> "test that"

irb(main):233:0> "test 123 test 123"[/\w+\s\d+/]
=> "test 123"
irb(main):248:0> "test 123 test 123"[/\w+\s\d+.*/]
=> "test 123 test 123"
irb(main):249:0> "test this test 123"[/\w+\s\d+.*/]
=> "test 123"
irb(main):250:0> "test this test that"[/\w+\s\d+.*/]
=> nil
irb(main):279:0> "try this test that"[/try\s\w[^\s]+/]
=> "try this"
irb(main):280:0> "try this test that"[/test\s\w[^\s]+/]
=> "test that"

irb(main):310:0> "try this test that"[/test\s\w\B+/]
=> "test tha"
irb(main):308:0> "try this test that"[/try\s\w\B+./]
=> "try this"
irb(main):309:0> "try this test that"[/test\s\w\B+./]
=> "test that"

irb(main):322:0> "try this test that"[/try\s\w+\b/]
=> "try this"
irb(main):323:0> "try this test that"[/test\s\w+\b/]
=> "test that"

irb(main):008:0> "try this test that"[/try.\b\w+/]
=> "try this"
irb(main):009:0> "try this test that"[/test.\b\w+/]
=> "test that"

irb(main):010:0> "try this test that"[/test.\w+/]
=> "test that"
irb(main):011:0> "try this test that"[/try.\w+/]
=> "try this"

irb(main):012:0> "try this test that"[/try\s\w+/]
=> "try this"
irb(main):013:0> "try this test that"[/test\s\w+/]
=> "test that"

irb(main):015:0> "try this test that"[/try\s\w+\s\w+/]
=> "try this test"

irb(main):041:0> "try this test that"[/\w+\s\w+$/]
=> "test that"
irb(main):043:0> "try this test that"[/^\w+\s\w+/]
=> "try this"



Note: Similar to CSS the more specific an expression or selector is the less ambiguous it is.

Calendar Loops

this program is very usefull when you need to know the list of dates of a repetitive task in the week.

I create this program to fill the records of my teaches in a school.

#include <stdio.h>


int meses[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int dias[365][3];
/*
0 Lunes
1 Martes
2 Miercoles
3 Jueves
4 Viernes
5 Sabado
6 Domingo
*/

int cursos[7][4]={{0,0,0,0},{2,2,0,0},{1,0,1,1},{0,1,0,0},{0,0,2,2},{0,0,0,0},{0,0,0,0}};

int main(){
    int i,j,k;
    dias[0][0]=5;
    for (i=1;i<365;i++){
        dias[i][0]=(dias[i-1][0]+1)%7;                       
    }    
    k=0;
    for (i=0;i<12;i++){
        for (j=1;j<=meses[i];j++){
            dias[k][1]=i;    
            dias[k][2]=j;
            k++;
        }
    }
    
    for (i=0;i<365;i++){
       // bool salto=false;
        for (j=0;j<4;j++){

            for (k=0;k<cursos[dias[i][0]][j];k++){
                printf("6-%c \t %i - %i \n",'A'+j,dias[i][2]+1,dias[i][1]+1);
//                salto=true;
            }               
        }    
       // if (salto)
       //    printf("\n");
    }
    
    return 0;
}

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS