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/.
[\* ]*Date\s*:\s*([0-9/]+)[ \*]*time\s*:\s*([0-9:]+)[ \*]*
in replace statement u use $1 $2 then you will get what you want
What about:
/DATE\s+:\s+([0-9/]+).+TIME\s+:\s+([0-9:]+)/
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
Related
From the string I need received result like below (using regex).
string: [Raxy] GTS_R-1/GTS_CNF-1/NA-15/SDA-1/MGTS-8002/PQM-1/APM-1/RF-3/NTL-15
result: [Raxy] MGTS-8002/PQM-1/APM-1
I know how to get:
"[Raxy] " -> \[.+\][\s?]
"MGTS-8002/PQM-1/APM-1" -> MGTS-\d+/PQM-\d+/PQM-\d+/APM-\d+
but I have a problem how to combine them.
Use
/^(\[[^\]\[]+]).*\/(MGTS-\d+\/PQM-\d+\/APM-\d+)/
See proof. Extract captures from the match, and combine into a single string.
Here is a brief demo:
const string = "[Raxy] GTS_R-1/GTS_CNF-1/NA-15/SDA-1/MGTS-8002/PQM-1/APM-1/RF-3/NTL-15";
const match = /^(\[[^\]\[]+]).*\/(MGTS-\d+\/PQM-\d+\/APM-\d+)/.exec(string);
console.log(match[1] + " " + match[2]);
The confusing part is the number in the string field can be with leading zeros but my query param will not contain that
Object 1:
{
"_id" : ObjectId("5c3f6aec29c2e3193315b485"),
"flightCode" : "000541300157840"
}
Object 2:
{
"_id" : ObjectId("5c3f6aec29c2e3193315b485"),
"flightCode" : "00054130015784"
}
If my intent is to find flight code that matches number 54130015784, how will I write my query?
You need $regex operator with following regular expression:
var code = "541300157840";
var regex = "^0*" + code + "$"
db.col.find({ flightCode: { $regex: new RegExp(regex) } })
where * means that 0 occurs zero or more times which means that it works both for 000541300157840 and for 541300157840
If you think that your data would have text flight code so the string can be identified, we can use this.
Regex:
54130015784(?="\n)
Explanation:
Positive Lookahead (?="\n)
Assert that the Regex below matches
" matches the character " literally (case sensitive)
\n matches a line-feed (newline) character (ASCII 10)
Example:
https://regex101.com/r/sF0YfH/3
Let me know if it works. If not give a clear idea what you want.
Suppose I had a string like so:
x <- "i2: 32390. 2093.32: "
How would I return a vector that would give me the positions of where a number is followed by a : or a . followed by a space?
So for this string it would be
"2: ","0. ","2: "
The regex you need is just '\\d[\\.:]\\s'. Using stringr's str_extract_all to quickly extract matches:
library(stringr)
str_extract_all("i2: 32390. 2093.32: ", '\\d[\\.:]\\s')
produces
[[1]]
[1] "2: " "0. " "2: "
You can use it with R's built-in functions, and it should work fine, as well.
What it matches:
\\d matches a digit, i.e. number
[ ... ] sets up a range of characters to match
\\. matches a period
: matches a colon
\\s matches a space.
I would like to clean this vector and only retain the digits
vec = c(" 4010 \"Filling in time budget diary\"", " 8888 \"Prob cont. preceding activity\"", " 9999 \"Missing, undecipherable\";")
what I would like is simply : 4010, 8888, 9999
I thought of something like, matching exactly the digits but it doesn't work.
gsub("^[[:digit:]]$", replacement = '', vec)
Thanks
We can use \\D+ to match all non-numeric elements and replace with ''
gsub('\\D+','', vec)
E.g. I want to match string with the same word at the end as at the begin, so that following strings match:
aaa dsfj gjroo gnfsdj riier aaa
sdf foiqjf skdfjqei adf sdf sdjfei sdf
rew123 jefqeoi03945 jq984rjfa;p94 ajefoj384 rew123
This one could do te job:
/^(\w+\b).*\b\1$/
explanation:
/ : regex delimiter
^ : start of string
( : start capture group 1
\w+ : one or more word character
\b : word boundary
) : end of group 1
.* : any number of any char
\b : word boundary
\1 : group 1
$ : end of string
/ : regex delimiter
M42's answer is ok except degenerate cases -- it will not match string with only one word. In order to accept those within one regexp use:
/^(?:(\w+\b).*\b\1|\w+)$/
Also matching only necessary part may be significantly faster on very large strings. Here're my solutions on javascript:
RegExp:
function areEdgeWordsTheSame(str) {
var m = str.match(/^(\w+)\b/);
return (new RegExp(m[1]+'$')).test(str);
}
String:
function areEdgeWordsTheSame(str) {
var idx = str.indexOf(' ');
if (idx < 0) return true;
return str.substr(0, idx) == str.substr(-idx);
}
I don't think a regular expression is the right choice here. Why not split the the lines into an array and compare the first and the last item:
In c#:
string[] words = line.Split(' ');
return words.Length >= 2 && words[0] == words[words.Length - 1];