Match the second word with no specific word behind - javascript

This is a follow-up of:
javascript regex - look behind alternative?
In my situation, I'm looking to only match the second word when there is no specific word that preceeds the term. As with the prior issue I need a solution that doesn't utilize the look behind technique.
I'm looking to exclude mentions such as the following:
patient has a history of pulmonary edema
Using the expression:
((?!pulmonary ).{10})\bedema
But given the following sentence:
Continuing dyspnea and lower-extremity edema
I would like the match to only return edema instead of extremity edema.

Please try this pattern:
(?!pulmonary).{10}\b(edema)\b
The demo is here.

Related

Get an example matched text from a regex pattern [duplicate]

Is there any way of generating random text which satisfies provided regular expression.
I am looking for a function which works like below
var reg = Some Regular Expression
var str = RandString(reg)
I have seen fairly good solutions in perl and ruby on github, but I think there are technical issues that make a complete solution impossible. For example, /[0-9]+/ has an infinite upper bound, which is not practical for selecting random numbers from.
Never seen it in JavaScript, but you could translate.
EDIT: After googling for a few seconds...
https://github.com/fent/randexp.js
if you know what the regular expression is, you can just generate random strings, then use a function that references the index of the letters and changes them as needed. Regex expressions vary widely, so it will be difficult to find one in particular that satisfies all possible regex.
Your question is pretty open so hopefully this steers you to the right solution. Get the current time (in seconds), MD5 it, check it against a REGEX, return the match.
Running Example: http://jsfiddle.net/MattLo/3gKrb/
Usage: RandString(/([A-Za-z])/ig); // expected to be a string
For JavaScript, the following modules can generate a random match to a regex:
pxeger
randexp.js
regexgen

Looking for a right regEx (Javascript)

I am looking for a right regex that would match "/anyword/" and perhaps "/ln/anyword/" in both:
http://localhost:3000/anyword
https://www.somedomain/ln/anyword
The "ln" in the second domain can be any two letter. (As languages).
(?:\w)(/.+) See https://regex101.com/r/3Ahynm/2
The capture group contains the desired text
What work as the above answer suggested:
/(?:\w)(\/.+)/i

Unable to determine the regex for a path format which could be stringA/stringB/StringX or stringA/stringX but not just stringA/stringB

This regex is in JavaScript. More specifically stringA = content, stringB = dam & stringx could be any string.
I have tried this regex & few others:
^\/(content(?!\/(dam)))\/(.*)
but this would recognize
/content/asfcew
/content/reddam
/content/usa/texas
and would not recognize
/content/dam
which is good, but alongside it also does not recognize
/content/dam/asdfafa
/content/damred
which is not good.
Any suggestions are much appreciated, thanks.
You just need to add an end-of-string anchor $ to the look-ahead:
^\/(content(?!\/(dam$)))\/(.*)
^
See demo
Now, (?!\/(dam$)) will only fail the match when dam appears before the end of string.
Note that there are too many capturing groups here, you may remove them like this:
^\/content(?!\/dam$)\/(.*)
See another demo
As the poster above said, you need an end of string anchor $ to the look ahead group.
To enable it capture both /content/dam and the rest use this pattern.
> ^\/(content(?=|\/(dam$)))\/(.*)
See demo here https://regex101.com/r/kO2cZ1/5

Regex to find web addresses in short copy

Having a short copy I need to match all occurrences of links to websites. To keep things simple a need to find out addresses in this format:
www.aaaaaa.bbbbbb
http://aaaaaa.bbbb
https://aa.bbbb
but also I need to take care of longer www/http/https versions:
www.aaaaa.bbbb.ccc.ddd.eeee
etc. So basically number of subdomains is not known. Now I came up with this regex:
(www\.([a-zA-Z0-9-_]|\.(?!\s))+)[\s|,|$]|(http(s)?:\/\/(?!\.)([a-zA-Z0-9-_]|\.(?!\s))+)[\s|,|$]
If you test on:
this is some tex with www.somewIebsite.dfd.jhh.hjh inside of it or maybe http://www.ssss.com or maybe https://evenore.com hahaah blah
It works fine with exception of when address is at the very end. $ seems to work only when there is \n in the end and it fails for:
this is some tex with www.somewIebsite.dfd.jhh.hjh
I'm guessing fix is simple and I miss something obvious so how would I fix it? BTW I posted regex here if yu want to quickly play around https://regex101.com/r/eL1bI4/3
The problem is that you placed the end anchor $ inside the character group []
[\s|,|$]
It is then interpreted literally as a dollar sign, and not as the anchor (the pipe character | is also interpreted literally, it's not needed there). The solution is to move the $ anchor outside:
(?:[\s,]|$)
However, in this case it makes more sense to use a positive lookahead instead of the noncapturing group (you don't want trailing spaces, or commas):
(?=[\s,]|$)
In the result you will end up with the following regex pattern:
(www\.([a-zA-Z0-9-_]|\.(?!\s))+)(?=[\s,]|$)|(http(s)?:\/\/(?!\.)([a-zA-Z0-9-_]|\.(?!\s))+)(?=[\s,]|$)
See the working demo.
The updated version that handles trailing full stops:
(www\.([a-zA-Z0-9-_]|\.(?!\s|\.|$))+)(?=[\s,.]|$)|(http(s)?:\/\/(?!\.)([a-zA-Z0-9-_]|\.(?!\s|\.|$))+)(?=[\s,.]|$)
See the working demo.

How to search csv string and return a match by using a Javascript regex

I'm trying to extract the first user-right from semicolon separated string which matches a pattern.
Users rights are stored in format:
LAA;LA_1;LA_2;LE_3;
String is empty if user does not have any rights.
My best solution so far is to use the following regex in regex.replace statement:
.*?;(LA_[^;]*)?.*
(The question mark at the end of group is for the purpose of matching the whole line in case user has not the right and replace it with empty string to signal that she doesn't have it.)
However, it doesn't work correctly in case the searched right is in the first position:
LA_1;LA_2;LE_3;
It is easy to fix it by just adding a semicolon at the beginning of line before regex replace but my question is, why doesn't the following regex match it?
.*?(?:(?:^|;)(LA_[^;]*))?.*
I have tried numerous other regular expressions to find the solution but so far without success.
I am not sure I get your question right, but in regards to the regular expressions you are using, you are overcomplicating them for no clear reason (at least not to me). You might want something like:
function getFirstRight(rights) {
var m = rights.match(/(^|;)(LA_[^;]*)/)
return m ? m[2] : "";
}
You could just split the string first:
function getFirstRight(rights)
{
return rights.split(";",1)[0] || "";
}
To answer the specific question "why doesn't the following regex match it?", one problem is the mix of this at the beginning:
.*?
eventually followed by:
^|;
Which might be like saying, skip over any extra characters until you reach either the start or a semicolon. But you can't skip over anything and then later arrive at the start (unless it involves newlines in a multiline string).
Something like this works:
.*?(\bLA_[^;]).*
Meaning, skip over characters until a word boundary followed by "LA_".

Categories

Resources