• Post Reply Bookmark Topic Watch Topic
  • New Topic

help in regex  RSS feed

 
Ajay Singh
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
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,

ajay
 
Campbell Ritchie
Marshal
Posts: 56209
171
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!