• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Need help with regular expression

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to develop a regular expression to parse an input file that contains multiple choice questions. The input is like this:



To begin with I have come up the following expression:



I realize that the above it probably not a good expression and I want to improve on this. But before that I am unable to figure out why this is not working. The problem that I am facing is that it correctly matches all the groups except for the fourth option. I mean, I see all the groups from 1-10 but the 10th group, which should contain the text for option (d), is empty. I don't understand why. All other groups contains correct values. for example,
1 = 1.
2 = this is question text. it can have multiple
lines like
this.
3 = (a)
and so on
10 = (empty).

This is happening for all the questions in the input text.


Please help!
 
Sheriff
Posts: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Here the .*? will act reluctantly and not match anything. You need to force it to match everything between (d) and the \n at the end...
 
Ann Basso
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ankit Garg wrote:
Here the .*? will act reluctantly and not match anything. You need to force it to match everything between (d) and the \n at the end...



Thanks for reply. I realize that I have to do that. But how? There can be multiple new lines in option 4. Also, why is working with other options??
 
Ankit Garg
Sheriff
Posts: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So there is nothing that can indicate where a question ends?? And your regular expression matches a question at one time it doesn't match 10 times in one question i.e. all the text from 1. to (d) is a single match, you wrote that you are getting matches as follows

1 = 1.
2 = this is question text. it can have multiple
lines like
this.
3 = (a)
and so on
10 = (empty).

 
Well don't expect me to do the dishes! This ad has been cleaned for your convenience:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!