vendredi 13 mars 2015

How to parse string and consume it according to different defined patterns in java?

I have a case where the user should define different patterns for a string like places locations in a configuration file, for example:



<place-name>
<place-name>, <place-location>
<place-name>, <place-location>, <city>
<place-name>, <place-location>, <city>, <country>
<place-name>, <place-location>, <country>


NOTE:The pattern shouldn't be limited to the above patterns only, it should be defined dynamically by the user as a configuration and it can be extended to tens of patterns.


Some of tags have a list of possible values to be able to differentiate between patterns with the same structure like the 3rd and 5th patterns where the user should define a list of possible values for city and country, for example



<city> = Abilene | Akron | Albuquerque | Alexandria | Allentown | Anaheim | Anchorage | Ann Arbor | Antioch | Arlington | Arvada| Aurora | Austin | Bakersfield| Baltimore | Bangor ...

<country> = USA | U.S. | United States | U.K. ...


According to these configuration, the end user should be able to write a string then it should be parsed by the program according to one of the defined patterns.


For example, if the end user write:



Disneyland Park, 1313 Disneyland Dr. , Anaheim, United States


The tool should be able to match it with the below pattern



<place-name>, <place-location>, <city>, <country>


Then these info. should be used later in the program for further processing.


How this issue could be handled in java? and Is there a straightforward way for that?


Aucun commentaire:

Enregistrer un commentaire