I'm hoping someone can help with some regular expression. I'm trying find instances where two patterns exist in a string (I think I'm saying that right).
Here is my test string:
{"eventid": 2121, "username":"FRED", "starttime": "1550243080", "newprocessname": "C:\\Windows\\System32\\wbem\\WmiPrvSE.exe", "parentprocessname": "C:\\Windows\\System32\\svchost.exe"}
I want to be able to search based on one or more criteria. The problem I have is when multiple criteria is provided. For example, the following seems to match if username is good or newprocessname is good whereas I want it to match only if both are good.
("username"\s*:\s*"(.*?)FRED(.*?)")|("newprocessname"\s*:\s*"(.*?)WINDOWS(.*?)")
I think my patterns are right, but how do I return a result only if both patterns match?
I hope I'm explaining this correct???
Thank you!
Jon
If you're using a regex library that supports lookaheads you can use this regex:
^(?=.*"username"\s*:\s*"([^"]*)FRED([^"]*)")(?=.*"newprocessname"\s*:\s*"([^"]*)WINDOWS([^"]*)")
It looks for both the username and newprocessname tags at the same time (as the lookaheads don't consume any characters). Note that you need to change .*? in your regex to [^"]* otherwise you can match text further into the string than the actual value associated with the tag name.
Demo on regex101
I am trying to write a regular expression that would match pairs of Ms and up to 3 Ns consecutively in any order so
MMMMNN would match
MMNNN would match
NNNMM would match
NMMMM would also match
I used the following regular expression:
((MM)*N{1,3})|(N{1,3}(MM)*)
each term matches alone but when I put the | between them it doesn't seem to match both possibilities. I used http://regex101.com/ to test it.
What regular expression would match those?
This matches all the examples you have:
(N{1,3}(MM)+)|((MM)+N{1,3})
The question is however, if 'up to 3' should include zero instances?
Edit: The comment is correct, removed the extra plus.
Does this work for you...
(MMN{0,3})|(N{0,3}MM)
I'm trying to get "10086" (the 'todId' next to "PENDING") out of the following text:
... [{"todId":10083,"torId":10013,"t":"VAC","c":"N","st":"APPROVED","s":null,"e":null,"cr":"2015-07-06T13:12:02","r":null,"sc":"Test","ac":null}],null,null,[{"todId":10086,"torId":10016,"t":"VAC","c":"N","st":"PENDING" ...
Please note, there is more characters before and after the above text.
How can I extract it? What can be the correct regular expression?
Thanks in advance!
--Ishti
UPDATE: Here is the complete text:
{"rows":{"904218":[null,null,null,null,null,null,[{"todId":10083,"torId":10013,"t":"VAC","c":"N","st":"APPROVED","s":null,"e":null,"cr":"2015-07-06T13:12:02","r":null,"sc":"Test","ac":null}],null,null,[{"todId":10086,"torId":10016,"t":"VAC","c":"N","st":"PENDING","s":null,"e":null,"cr":"2015-07-06T13:50:05","r":null,"sc":"Test","ac":null}],null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]},"holidays":{},"l10n":{"TOP_APPROVED":"Approved","TOFFTYP_VAC":"VAC","TOP_PENDING":"Requested"}}
Use the following regex:
(?:(?:"todId"\:([0-9]+),.+?)+?PENDING.+?)+?
It will match what you are looking for. The captured group will always contain the nearest 'todId' next to the text "PENDING"
Here is the demo link for you to see the regex in action.
Does this one helps?
(?:todId)+.+\"todId\":([0-9]+).+PENDING
I was trying to use both pcre and re2 and I came up with the following observation.
When I give the string as
"ab cd"
and the pattern as
"^[^c]"
re2 returns NO MATCH but its actually a match.
That is to say when I type this RE2::FullMatch("ab cd", RE2("^[^c]")) I get FAIL/No Match.
Please let me know if I am going wrong somewhere or what is the problem?
RE2::FullMatch matches the entire string, like Jerry says.
There are two basic operators: RE2::FullMatch requires the regexp to
match the entire input text, and RE2::PartialMatch looks for a match
for a substring of the input text, returning the leftmost-longest
match in POSIX mode and the same match that Perl would have chosen in
Perl mode.
https://code.google.com/p/re2/wiki/CplusplusAPI
Q1: I'm writing a regex in php and not successful. I want to match the following:
so i would
if not then match:
so i
and then:
i would
and
so
i
would
Here is my code:
\b(so i|i would|so i would|(so|i|would))\b
Its only matching the: so, i, would, so i, i would .... but not matching the so i would?
Order your regex correctly.
\b(so i would|so i|i would|(so|i|would))\b
Put the longest string to match to the left.
The | is left-associative and hence, in your version Of the regex, is matching the shorter string.
Just put it at the beginning
\b(so i would|so i|i would|(so|i|would))\b
put longest pattern to left in the group: \b(long|...|short)\b
another solution: \b(so i would|i would|would|so i|so|i)\b
p.s. this is NFA regex engine feature, please refer to "Mastering Regular Expressions"