Quantcast

How to find out if a string contains a pattern or not?

classic Classic list List threaded Threaded
17 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to find out if a string contains a pattern or not?

Jason
Hi gurus -

I want to find a date from a string.
Please advise.

My scala code like this -

def todayOrTomorrow = rule {
      (zeroOrMore(ANY)~(tomorrow | today)~zeroOrMore(ANY)~EOI)
    }
    def today=rule {str("today")~>withContext({
          (string,context)=>
          println("string " + string)
          println("context " +context.getStartIndex)
          string
        })}
    def tomorrow=rule {str("tomorrow")~>withContext({
          (string,context)=>
          println("string " + string)
          println("context " +context.getStartIndex)
          string
        })}

when run with "I will do it tomorrow.", i got error -

org.parboiled.errors.ParsingException: Invalid calculation expression:
Invalid input 'EOI', expected ANY, tomorrow or today (line 1, pos 23):
I will do it tomorrow.
                      ^

        at ruleparser.TimeParser.parse(TimeParser.scala:40)
        at ruleparser.Main$.main(Main.scala:14)
        at ruleparser.Main.main(Main.scala)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:616)
        at sbt.Run.run0(Run.scala:60)
        at sbt.Run.execute$1(Run.scala:47)
        at sbt.Run$$anonfun$run$2.apply(Run.scala:50)
        at sbt.Run$$anonfun$run$2.apply(Run.scala:50)
        at sbt.TrapExit$.executeMain$1(TrapExit.scala:33)
        at sbt.TrapExit$$anon$1.run(TrapExit.scala:42)


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to find out if a string contains a pattern or not?

mathias
Administrator
The zeroOrMore and oneOrMore constructs in Parsing Expression Grammars are always greedy.
This means that your "zeroOrMore(ANY)" will read all characters up to the final character which is rarely what you want.

You probably want:

(zeroOrMore(!(tomorrow | today) ~ ANY) ~ (tomorrow | today) ~ zeroOrMore(ANY) ~ EOI)

Which basically says: Match everything that is NOT (tomorrow | today), than match (tomorrow | today) than match everything else.

Cheers,
Mathias
---
[hidden email]
http://www.parboiled.org

On 10.11.2010, at 15:32, Jason [via parboiled users] wrote:

> Hi gurus -
>
> I want to find a date from a string.
> Please advise.
>
> My scala code like this -
>
> def todayOrTomorrow = rule {
>       (zeroOrMore(ANY)~(tomorrow | today)~zeroOrMore(ANY)~EOI)
>     }
>     def today=rule {str("today")~>withContext({
>           (string,context)=>
>           println("string " + string)
>           println("context " +context.getStartIndex)
>           string
>         })}
>     def tomorrow=rule {str("tomorrow")~>withContext({
>           (string,context)=>
>           println("string " + string)
>           println("context " +context.getStartIndex)
>           string
>         })}
>
> when run with "I will do it tomorrow.", i got error -
>
> org.parboiled.errors.ParsingException: Invalid calculation expression:
> Invalid input 'EOI', expected ANY, tomorrow or today (line 1, pos 23):
> I will do it tomorrow.
>                       ^
>
>         at ruleparser.TimeParser.parse(TimeParser.scala:40)
>         at ruleparser.Main$.main(Main.scala:14)
>         at ruleparser.Main.main(Main.scala)
>         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
>         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>         at java.lang.reflect.Method.invoke(Method.java:616)
>         at sbt.Run.run0(Run.scala:60)
>         at sbt.Run.execute$1(Run.scala:47)
>         at sbt.Run$$anonfun$run$2.apply(Run.scala:50)
>         at sbt.Run$$anonfun$run$2.apply(Run.scala:50)
>         at sbt.TrapExit$.executeMain$1(TrapExit.scala:33)
>         at sbt.TrapExit$$anon$1.run(TrapExit.scala:42)
>
>
>
>
> View message @ http://users.parboiled.org/How-to-find-out-if-a-string-contains-a-pattern-or-not-tp1876350p1876350.html 
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, click here.
>

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to find out if a string contains a pattern or not?

Jason
Thank you for really rapid reply.
This works.
But this rule looks costly, any thought?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to find out if a string contains a pattern or not?

mathias
Administrator
What do you mean by costly?

---
[hidden email]
http://www.parboiled.org

On 10.11.2010, at 16:07, Jason [via parboiled users] wrote:

> Thank you for really rapid reply.
> This works.
> But this rule looks costly, any thought?
>
> View message @ http://users.parboiled.org/How-to-find-out-if-a-string-contains-a-pattern-or-not-tp1876350p1876544.html 
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, click here.
>

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to find out if a string contains a pattern or not?

iubito
In reply to this post by mathias
Hi
I'm curious, how would you write such rule
(zeroOrMore(!(tomorrow | today) ~ ANY) ~ (tomorrow | today) ~ zeroOrMore(ANY) ~ EOI)

in java?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to find out if a string contains a pattern or not?

mathias
Administrator
Sequence(
    ZeroOrMore(
        TestNot(FirstOf(Tomorrow(), Today())),
        ANY
    ),
    FirstOf(Tomorrow(), Today()),
    ZeroOrMore(ANY),
    EOI
)

---
[hidden email]
http://www.parboiled.org

On 26.11.2010, at 16:05, iubito [via parboiled users] wrote:

> Hi
> I'm curious, how would you write such rule
> (zeroOrMore(!(tomorrow | today) ~ ANY) ~ (tomorrow | today) ~ zeroOrMore(ANY) ~ EOI)
>
> in java?
>
> View message @ http://users.parboiled.org/How-to-find-out-if-a-string-contains-a-pattern-or-not-tp1876350p1973166.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, click here.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to find out if a string contains a pattern or not?

cosmindumy
This post was updated on .
In reply to this post by mathias
Hi,
Ungreedy isn't implemented in parboiled?
I want to match a specific word among other words?
How do I do that?
For example I want to match 'd!' among a text.
Thanks.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to find out if a string contains a pattern or not?

mathias
Administrator
You'll have to use a syntactic predicate. For example:

    Rule NoColorText() {
      return Sequence(TestNot(Color()), ANY);
    }

    Rule Color() {
      return FirstOf("red", "green", "blue");
    }

HTH and cheers,
Mathias

---
[hidden email]
http://www.parboiled.org

On 12.11.2012, at 12:17, cosmindumy [via parboiled users] <[hidden email]> wrote:

> Hi,
> Ungreedy isn't implemented in parboiled.
> I want to match a specific word among other words?
> How do I do that?
> For example I want to match 'd!' among a text.
> Thanks.
>
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/How-to-find-out-if-a-string-contains-a-pattern-or-not-tp1876350p4024087.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, click here.
> NAML

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to find out if a string contains a pattern or not?

cosmindumy
Hi,
Thanks for your answers. Now I'm implementing a command recognition and this library helps me.
Regards.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to find out if a string contains a pattern or not?

cosmindumy
In reply to this post by cosmindumy
Hi,
I have another question. I want to try to match some strings,  for example Yesterday(), Today(), Tomorrow(), none is not mandatory and also the order.  Can this be made in a single rule, and one parse result?
This strings are independent of each other.
I defined my Yesrday rule like Sequence(!Yesterday,Yesterday,ZeroOrMore(ANY)). and it consumes the whole input.

 I know that this is not the scope of per grammar, but can I simulate this behaviour?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to find out if a string contains a pattern or not?

mathias
Administrator
There was a similar question a while ago:
http://users.parboiled.org/Re-Best-way-to-have-sequence-of-optional-tokens-with-separator-inbetween-td4024038.html

I think the discussion would also apply in your case.

Cheers,
Mathias

---
[hidden email]
http://www.parboiled.org

On 22.11.2012, at 11:57, cosmindumy [via parboiled users] <[hidden email]> wrote:

> Hi,
> I have another question. I want to try to match some strings,  for example Yesterday(), Today(), Tomorrow(), none is not mandatory and also the order.  Can this be made in a single rule, and one parse result?
> This strings are independent of each other.
> I defined my Yesrday rule like Sequence(!Yesterday,Yesterday,ZeroOrMore(ANY)). and it consumes the whole input.
>
>  I know that this is not the scope of per grammar, but can I simulate this behaviour?
>
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/How-to-find-out-if-a-string-contains-a-pattern-or-not-tp1876350p4024099.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, click here.
> NAML

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to find out if a string contains a pattern or not?

macdonald.rich
I couldn't quite understand the "none is not mandatory and also the order". Assuming this means "each item is optional and in any order, and not checking for duplicates", how about this simple pattern:

--------------
Write Yesterday(), Today(), Tomorrow() rules to match a single occurrence.

--------------
Rule YesterdayTodayTomorrow(){
   return FirstOf(
      Yesterday(),
      Today(),
      Tomorrow()
   );
}

--------------
Rule MainRule(){
   return oneOrMoreSeparatedBy(
      YesterdayTodayTomorrow(),
      SeparatorRule()
);

----------------
Rule oneOrMoreSeparatedBy(Object ruleItem, Object separator){
   return Sequence(ruleItem, ZeroOrMore(separator, ruleItem);
}


On Fri, Nov 23, 2012 at 2:24 AM, mathias [via parboiled users] <[hidden email]> wrote:
There was a similar question a while ago:
http://users.parboiled.org/Re-Best-way-to-have-sequence-of-optional-tokens-with-separator-inbetween-td4024038.html

I think the discussion would also apply in your case.

Cheers,
Mathias

---
[hidden email]
http://www.parboiled.org


On 22.11.2012, at 11:57, cosmindumy [via parboiled users] <[hidden email]> wrote:

> Hi,
> I have another question. I want to try to match some strings,  for example Yesterday(), Today(), Tomorrow(), none is not mandatory and also the order.  Can this be made in a single rule, and one parse result?
> This strings are independent of each other.
> I defined my Yesrday rule like Sequence(!Yesterday,Yesterday,ZeroOrMore(ANY)). and it consumes the whole input.
>
>  I know that this is not the scope of per grammar, but can I simulate this behaviour?
>
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/How-to-find-out-if-a-string-contains-a-pattern-or-not-tp1876350p4024099.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, click here.
> NAML




If you reply to this email, your message will be added to the discussion below:
http://users.parboiled.org/How-to-find-out-if-a-string-contains-a-pattern-or-not-tp1876350p4024102.html
To start a new topic under parboiled users, email [hidden email]
To unsubscribe from parboiled users, click here.
NAML

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to find out if a string contains a pattern or not?

mathias
Administrator
Yes, that looks like a good solution on the syntax (i.e. parser level).
On a higher level in your application you would then check, whether the list of days is semantically correct (e.g., doesn't contain duplicates).

Cheers,
Mathias

---
[hidden email]
http://www.parboiled.org

On 23.11.2012, at 23:31, "macdonald.rich [via parboiled users]" <[hidden email]> wrote:

> I couldn't quite understand the "none is not mandatory and also the order". Assuming this means "each item is optional and in any order, and not checking for duplicates", how about this simple pattern:
>
> --------------
> Write Yesterday(), Today(), Tomorrow() rules to match a single occurrence.
>
> --------------
> Rule YesterdayTodayTomorrow(){
>    return FirstOf(
>       Yesterday(),
>       Today(),
>       Tomorrow()
>    );
> }
>
> --------------
> Rule MainRule(){
>    return oneOrMoreSeparatedBy(
>       YesterdayTodayTomorrow(),
>       SeparatorRule()
> );
>
> ----------------
> Rule oneOrMoreSeparatedBy(Object ruleItem, Object separator){
>    return Sequence(ruleItem, ZeroOrMore(separator, ruleItem);
> }
>
>
> On Fri, Nov 23, 2012 at 2:24 AM, mathias [via parboiled users] <[hidden email]> wrote:
> There was a similar question a while ago:
> http://users.parboiled.org/Re-Best-way-to-have-sequence-of-optional-tokens-with-separator-inbetween-td4024038.html
>
> I think the discussion would also apply in your case.
>
> Cheers,
> Mathias
>
> ---
> [hidden email]
> http://www.parboiled.org
>
>
> On 22.11.2012, at 11:57, cosmindumy [via parboiled users] <[hidden email]> wrote:
>
> > Hi,
> > I have another question. I want to try to match some strings,  for example Yesterday(), Today(), Tomorrow(), none is not mandatory and also the order.  Can this be made in a single rule, and one parse result?
> > This strings are independent of each other.
> > I defined my Yesrday rule like Sequence(!Yesterday,Yesterday,ZeroOrMore(ANY)). and it consumes the whole input.
> >
> >  I know that this is not the scope of per grammar, but can I simulate this behaviour?
> >
> > If you reply to this email, your message will be added to the discussion below:
> > http://users.parboiled.org/How-to-find-out-if-a-string-contains-a-pattern-or-not-tp1876350p4024099.html
> > To start a new topic under parboiled users, email [hidden email]
> > To unsubscribe from parboiled users, click here.
> > NAML
>
>
>
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/How-to-find-out-if-a-string-contains-a-pattern-or-not-tp1876350p4024102.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, click here.
> NAML
>
>
>
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/How-to-find-out-if-a-string-contains-a-pattern-or-not-tp1876350p4024103.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, click here.
> NAML

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to find out if a string contains a pattern or not?

cosmindumy
Hi,
I cannot implement a rule that seems to be a little bit more complicated.
Given the text "This text is in english. Translate in german." I want to match the destination language (gernan). First part can be missing.
I want to implement a rule that matches 'in language' not preceded by any of words 'is' or 'are'.  
I tried something like: (Syntax is not accurate)

Precontext=  Sequence(TestNot(String("is")), String("in"))
Value = Language();
Base Rule = Sequence(ZeroOrMore(TestNot(Precontext),ANY),Precontext(), Value),
It doesnt work. Checking parse tree I realized that ZeroOrMore(TestNot(Precontext),ANY) cnsumes also string 'is'.
Can you give me an ideea?
Thanks.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to find out if a string contains a pattern or not?

mathias
Administrator
> Checking parse tree I realized that
> ZeroOrMore(TestNot(Precontext),ANY) cnsumes also string 'is'.

`ZeroOrMore(TestNot(Precontext),ANY)` will match `is` if it is not followed by `in`.
For example, `is foo` will not be matched by the Precontext rule thereby making the `TestNot(Precontext)` match and the `ANY` consume the `is`.

HTH and cheers,
Mathias

---
[hidden email]
http://www.parboiled.org

On 30.01.2013, at 14:54, "cosmindumy [via parboiled users]" <[hidden email]> wrote:

>
>
> Hi,
> I cannot implement a rule that seems to be a little bit more complicated.
> Given the text "This text is in english. Translate in german." I want to
> match the destination language (gernan). First part can be missing.
> I want to implement a rule that matches 'in language' not preceded by any of
> words 'is' or 'are'.  
> I tried something like: (Syntax is not accurate)
>
> Precontext=  Sequence(TestNot(String("is")), String("in"))
> Value = Language();
> Base Rule = Sequence(ZeroOrMore(TestNot(Precontext),ANY),Precontext(),
> Value),
> It doesnt work. Checking parse tree I realized that
> ZeroOrMore(TestNot(Precontext),ANY) cnsumes also string 'is'.
> Can you give me an ideea?
> Thanks.
>
>
>
> _______________________________________________
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/How-to-find-out-if-a-string-contains-a-pattern-or-not-tp1876350p4024141.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, visit
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to find out if a string contains a pattern or not?

cosmindumy
Sorry, but i didn't get the ideea.
My base rule is a general one and cannot be modified.
I have to implement Precontext to match only 'in' but not 'is in' or 'are in'.
In my case, precontext matches 'in language' and Not(precontext) matches until the word 'is' from text 'This text is in english'
As precontext matches 'in' Not(Precontext) wil always match until it.
I tryed the following rule() :

Precontext = Sequence(OneOrMore(TestNot(FirstOf(Word("is"), Word("in"))), ANY), Word("in"));  
meaning match anythng that is not 'is' or 'in' followed by 'in'. Now from text: 'This text is in english'
Not matches 'This text i'
Precontext: 's in'
Language 'english'
wich I don't want.
Thanks.  
 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to find out if a string contains a pattern or not?

mathias
Administrator
The problem is that you are "excluding" things on a word-basis but then consume input in a character basis.
Try this:

    Precontext = Sequence(OneOrMore(TestNot(FirstOf(Word("is"), Word("in"))), AnyWord), Word("in"));  

Cheers,
Mathias

---
[hidden email]
http://www.parboiled.org

On 31.01.2013, at 14:09, cosmindumy [via parboiled users] <[hidden email]> wrote:

>
>
> Sorry, but i didn't get the ideea.
> My base rule is a general one and cannot be modified.
> I have to implement Precontext to match only 'in' but not 'is in' or 'are
> in'.
> In my case, precontext matches 'in language' and Not(precontext) matches
> until the word 'is' from text 'This text is in english'
> As precontext matches 'in' Not(Precontext) wil always match until it.
> I tryed the following rule() :
>
> Precontext = Sequence(OneOrMore(TestNot(FirstOf(Word("is"), Word("in"))),
> ANY), Word("in"));  
> meaning match anythng that is not 'is' or 'in' followed by 'in'. Now from
> text: 'This text is in english'
> Not matches 'This text i'
> Precontext: 's in'
> Language 'english'
> wich I don't want.
> Thanks.  
>
>
>
>
> _______________________________________________
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/How-to-find-out-if-a-string-contains-a-pattern-or-not-tp1876350p4024144.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, visit
Loading...