• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

help in regex

Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have the following set of statements
1)I am buying a mobile
2)i am buying a tv in usa.
3)i am buying a potato
4)i am buying a tv in dreams.

I have a regex group called electronicItems which has mobile,tv,referigerator etc.Also i have a group for countries which is optional.The word
"buying" is compulsory in my statement and the electronicItem group is also compulsory.so based on these conditions only the first two statements should be matched.
so i have written the following regex

(I)?.*(buying).*(mobile|tv).*(usa|uk)? --->this matches 1st,2nd and 4th statements

Also i am not able to see the country group using matcher.group(4) function.

i want to match only the first two statments along with the captured groups,how do i achieve this?please guide.

thanks in advance,

Posts: 76887
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you may be moving beyond the sphere of regular grammars. Get a copy of "Lex and Yacc" by Mason Brown and Levine (written about 1992) and there are examples of similar grammars using lex to identify different tokens.
I am surprised you can match the second statement, which has a final full stop which your grammar doesn't allow.
Actually that stop is permitted as part of .*
Your .* allows you to match any number (Including 0) of any character, except line end. This may or may not include uk and usa. So you can match the 4th statement as well " in dreams." counts as an example of .*

I think this question is too difficult for "beginning", so I shall move it.
Do the next thing next. That’s a pretty good rule. Read the tiny ad, that’s a pretty good rule, too.
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic