Rodrigo Schemes
Rodrigo Schemes

Reputation: 123

Regular Expression: Remove the middle string

I have the following sentence:

**     DATE : 04/12/2014   * TIME: 07:49:42                **

I only want to capture 04/12/2014 07:49:42.

I've tried this .*DATE : ([0-9\/]+.*TIME: [0-9\:]+)

But I got this: "04/12/2014 * TIME: 07:49:42."

How can I remove " * TIME:"?

I need it in pure regex, so I'm testing at http://www.regexr.com/.

Upvotes: 3

Views: 4102

Answers (3)

parthi
parthi

Reputation: 734

[\* ]*Date\s*:\s*([0-9/]+)[ \*]*time\s*:\s*([0-9:]+)[ \*]*

in replace statement u use $1 $2 then you will get what you want

Upvotes: 1

royhowie
royhowie

Reputation: 11171

This should do the trick:

/\*\* +DATE : ([\d|\/]+) +\* TIME: ([\d|:]+) +\*\*/

This will return a tuple. So, for example, using JavaScript:

var s = "**     DATE : 04/12/2014   * TIME: 07:49:42                **",
    re = /\*\* +DATE : ([\d|\/]+) +\* TIME: ([\d|:]+) +\*\*/;

re.exec(s);  // returns ["original string", "04/12/2014", "07:49:42"]

breakdown:

\*\* +DATE : (note the space after the colon) matches up to "DATE : "

([\d|\/]+) matches numbers and slashes and captures them as the first group.

+\* TIME: matches up to "TIME: "

([\d|:]+) captures the time by matching numbers or colons

+\*\*/ finishes off the sequence

Upvotes: 0

sebnukem
sebnukem

Reputation: 8323

What about:

/DATE\s+:\s+([0-9/]+).+TIME\s+:\s+([0-9:]+)/

Upvotes: 0

Related Questions