tags:

views:

35

answers:

2

Hi All, I have a version like this “5.3.08.01”, I want to split this version string into four so that each digit get assigned to variables, means it should be like this:

A= 5

B=3

C=08

D=01

I tried doing like this pattern="(\d*).(\d*).(\d*).(\d*)"

above expression gives me first digit “5”, now how to get rest of the digits? Can anyone help me out on this I will be thankful to you

+3  A: 

You need to escape the dot (.), and I'd use a + instead of * to make it at least one digit:

(\d+)\.(\d+)\.(\d+)\.(\d+)
Lucero
I am using xmltask in ANT, so this is something I am doing by reading node value from xml file and taking the value in property<xmltask source="${act.dir.path}/pom.xml"> <regexp path="//:project/:version[1]/text()" pattern="(\d+)\.(\d+)\.(\d+)\.(\d+)" property="servlet.name"/> <regexp path="//:project/:version[1]/text()" pattern="???" property="servlet.name2"/></xmltask>Property “servlet.name” gives me first digit “5”, but how to get other values? Like “servlet.name2” should be “3”, “servlet.name3” should be “08” and “servlet.name4” should be “01” Is this possible in regexp?
saharey
You need to specify a `replace="$1"` attribute. The `$x` inserts the text you matched (where x is the index to the group, starting with 1). Have a look at the docs! http://www.oopsconsultancy.com/software/xmltask/ for instance.
Lucero
A: 

language is not specified so I can suggest java solution (and I'm pretty sure that c# has similar one):

String ip = "“5.3.08.01";
String[] nums = ip.split ("."); //array of 4 elements
John Doe
Java's `String#split(delim)` method treats `delim` as a regex, so you would need to escape the dot: `ip.split("\\.")` C# (.NET) has both a `String#Split` (which takes one or more literal delimiters in the form of a `String[]` or a `Char[]`) and a `Regex#Split` (which, like Java's `split()`, would require you to escape the dot: `@"\."`).
Alan Moore