DevRes2721608
DevRes2721608

Reputation: 38

I want the regular expression for the data of type dd.d.dd.ddddd or dd.d.d.ddddd

I want the regular expression for the data of type dd.d.dd.ddddd or dd.d.d.ddddd,each d is for a digit between 0-9 and the regular expression should represent both the formats. I am working in java script.I have tried the following code.but it is not working for all the input strings.

<p id="demo">my string</p>
<button onclick="myFunction()">Try it</button>

<script>
function myFunction()
{
//var regExp1=/[1-4]+\.[1-4]+\.[1-4]+\.[0-9]+$/;

var regExp1=/[1-4][1-4]\.[1-4]\.[1-4][1-4]\.[0-9][0-9][0-9][0-9][0-9]$/;

//var regExp1=/[0-9][0-9]\.[0-9]\.[0-9][0-9]\.[0-9][0-9][0-9][0-9][0-9]$/;

var str="Version of C:\hjkl.dll:14.6.17.90505 working File Versn:18.1.9.17083,stopped file:13.1.14.25059 absjhdhgh";

var mystring=str.split(regExp1);
document.getElementById("demo").innerHTML=mystring;
}
</script>

Desired output is:

Version of C:\hjkl.dll:
 working File Versn:
,stopped file:
 absjhdhgh

Upvotes: 0

Views: 6581

Answers (3)

hsz
hsz

Reputation: 152266

Try with following regex:

\d{2}\.\d\.\d{1,2}\.\d{5}

Example:

var str     = "Version of C:\hjkl.dll:14.6.17.90505 working File Versn:18.1.9.17083,stopped file:13.1.14.25059 absjhdhgh";
var regExp1 = /\d{2}\.\d\.\d{1,2}\.\d{5}/;

var output  = str.split(regExp1);

Output:

["Version of C:hjkl.dll:", " working File Versn:", ",stopped file:", " absjhdhgh"]

To join array's elements, use:

var newString = output.join("\n"); // or <br/> instead of \n

Upvotes: 5

Toto
Toto

Reputation: 91488

I'd use:

\b\d{2}\.\d\.\d{1,2}\.\d{5}\b

Upvotes: 1

MDEV
MDEV

Reputation: 10838

Just match the simpler version and add an optional 2nd digit in the third group with \d?

\d\d\.\d\.\d\d?\.\d{5}

Upvotes: 1

Related Questions