This is similar to javascript regular expression to not match a word, but the accepted answer shows a regex that doesn't match if the word is inside the line. I want to match everything except the full word. It will match "__lambda__".
This is also similar to my question Regex that match everything except the list of strings but that one gives a solution with String::split and I want to use normal match full string.
For example, it should match everything except /^lambda$/ I would prefer to have a regex for this, since it will fit my pattern patching function. It should match lambda_ or _lambda and everything else - except full "lambda" token.
I've tried this:
/^(.(?!lambda))+$/
and
/^((?!lambda).)+$/
this works
/^(.*(?<!lambda))$/
but I would prefer to not have to use a negative lookbehind if I can avoid it (because of browser support). Also, I have interpreter written in JavaScript that I need this for and I would like to use this in guest language (I will not be able to compile the regex with babel).
Is something like this possible with regex without lookbehind?
EDIT:
THIS IS NOT DUPLICATE: I don't want to test if something contains or doesn't contain the word, but if something is not exact word. There are not question on Stack Overflow like that.
Question Regex: Match word not containing has almost as correct an answer as mine, but it's not the full answer to the question. (It would help in finding solution, though.)
I was able to find the solution based on How to negate specific word in regex?
var re = /^(?!.*\blambda\b).*$/;
var words = ['lambda', '_lambda', 'lambda_', '_lambda_', 'anything'];
words.forEach(word => {
console.log({word, match: word.match(re)});
});
Related
I want to append a word after <body> tag, it should not modify/replace anything other than just append a word. I have done something like this, is it valid do empty parenthesis fir second capture group will match everything?
/(<body[^>]*>)()/, `$1${my_variable}$2`)
The second capture group, designed to capture nothing, will match "nothing" - it will form a match immediately after your closed body tag. There's nothing wrong with doing this for the regex, though you might want to be wary of using [^>]* - this negated character class will gladly match across lines and grab as much input as it can. Handy for matching multi-line tags, but often very dangerous.
Also, if you're on linux and for some reason have > symbols in filenames (which is valid!) your regex will break horribly, as shown here.
That being said, valid regex or not, it's usually a bad idea to use regex with html, since HTML isn't a regular language. Also, you could accidentally summon Cthulhu.
let page = "<html><body>Some info</body></html>";
page.replace("<body>", `<body>${my_variable}`);
or
page.replace(/<body>|<BODY>/, `<body>${my_variable}`);
If in the broweser you can also use document.querySelector("body").innerHTML
Also depending on which framework you're using there are better ways to accomplish this.
I was a bit surprised, that actually no one had the exact same issue in javascript...
I tried several different solutions none of them parse the content correctly.
The closest one I tried : (I stole its regex query from a PHP solution)
const test = `abc?aaa.abcd?.aabbccc!`;
const sentencesList = test.split("/(\?|\.|!)/");
But result just going to be
["abc?aaa.abcd?.aabbccc!"]
What I want to get is
['abc?', 'aaa.', 'abcd?','.', 'aabbccc!']
I am so confused.. what exactly is wrong?
/[a-z]*[?!.]/g) will do what you want:
const test = `abc?aaa.abcd?.aabbccc!`;
console.log(test.match(/[a-z]*[?!.]/g))
To help you out, what you write is not a regex. test.split("/(\?|\.|!)/"); is simply an 11 character string. A regex would be, for example, test.split(/(\?|\.|!)/);. This still would not be the regex you're looking for.
The problem with this regex is that it's looking for a ?, ., or ! character only, and capturing that lone character. What you want to do is find any number of characters, followed by one of those three characters.
Next, String.split does not accept regexes as arguments. You'll want to use a function that does accept them (such as String.match).
Putting this all together, you'll want to start out your regex with something like this: /.*?/. The dot means any character matches, the asterisk means 0 or more, and the questionmark means "non-greedy", or try to match as few characters as possible, while keeping a valid match.
To search for your three characters, you would follow this up with /[?!.]/ to indicate you want one of these three characters (so far we have /.*?[?!.]/). Lastly, you want to add the g flag so it searches for every instance, rather than only the first. /.*?[?!.]/g. Now we can use it in match:
const rawText = `abc?aaa.abcd?.aabbccc!`;
const matchedArray = rawText.match(/.*?[?!.]/g);
console.log(matchedArray);
The following code works, I do not think we need pattern match. I take that back, I have been answering in Java.
final String S = "An sentence may end with period. Does it end any other way? Ofcourse!";
final String[] simpleSentences = S.split("[?!.]");
//now simpleSentences array has three elements in it.
I am building a graph drawer and currently working on the math expression parser. I'm done with most parts but I'm stuck at clearing the input text before parsing it. What I'm trying to achieve now is getting rid of unpermitted characters.
For example, in this text:
5ax+4asxxv+sdflog10aloga(132*43)sin(132)
I want to match everything that is not +,-,*,/,^,(,),ln,log,sin,cos,tan,cot,arcsin,arccos,...
and replace them with "".
so that the output is
5x+4xx+log10log(132*43)sin(132)
I need help with the regex.
Spaces don't matter since I clear them out beforehand.
A little bit tricky - at least I couldn't think of a simple way to do what you ask. The regex would get monstrous.
So I did it the other way around - match what you want to keep, and put it back together.
The regex:
[\d+*/^()x-]|ln|log|(?:arc)?(?:sin|cos)|tan|cot
The code:
var re = /[\d+*/^()x-]|ln|log|(?:arc)?(?:sin|cos)|tan|cot/g,
text = '5ax+4asxxv+sdflog10aloga(132*43)sin(132)arccos(1)';
console.log(text.match(re).join(''));
I want to get all the words, except one, from a string using JS regex match function. For example, for a string testhello123worldtestWTF, excluding the word test, the result would be helloworldWTF.
I realize that I have to do it using look-ahead functions, but I can't figiure out how exactly. I came up with the following regex (?!test)[a-zA-Z]+(?=.*test), however, it work only partially.
http://refiddle.com/refiddles/59511c2075622d324c090000
IMHO, I would try to replace the incriminated word with an empty string, no?
Lookarounds seem to be an overkill for it, you can just replace the test with nothing:
var str = 'testhello123worldtestWTF';
var res = str.replace(/test/g, '');
Plugging this into your refiddle produces the results you're looking for:
/(test)/g
It matches all occurrences of the word "test" without picking up unwanted words/letters. You can set this to whatever variable you need to hold these.
WORDS OF CAUTION
Seeing that you have no set delimiters in your inputted string, I must say that you cannot reliably exclude a specific word - to a certain extent.
For example, if you want to exclude test, this might create a problem if the input was protester or rotatestreet. You don't have clear demarcations of what a word is, thus leading you to exclude test when you might not have meant to.
On the other hand, if you just want to ignore the string test regardless, just replace test with an empty string and you are good to go.
I am trying to do something really really basic.
It's just a search and replace using this function, which uses some proprietary Regex I never used before.
https://developers.google.com/apps-script/reference/document/text#replaceText(String,String)
What I am trying to accomplish is simple, run through the whole document and replace placeholders with text.
The string to match is in this format:
#replace this please#
By using this pattern:
(\W|^)#replace this please#(\W|$)
copied from the Google Examples found here (https://support.google.com/a/answer/1371417?hl=en)
It works absolutely fine for one exception which bugs me out.
If I have 2 or more placeholders on the same line, it won't match any of them.
So if I have something like this:
#replace me please# and some normal text here #replace me too#
None of those 2 will be matched.
I am assuming my expression doesn't take this into account, but the documentation is very hard to find for their implementation of regular expressions.
Can anybody help please?
Having this line in the document:
You may try using the following regex replacement function:
function googleDocsApi27827395() {
var body = DocumentApp.getActiveDocument().getBody();
body.replaceText("(\\W|^)#replace this please#(\\W|$)", "");
}
The result:
The \\W also matches the adjacent symbol after the first and before the last search word and they are also removed. If you do not need that behavior, remove the (\\W|^) and (\\W|$).
In case you have 3 different strings in between #...#s, you can use alternations to build the regex:
body.replaceText("#(replace this please|replace me (please|too))#", "");
This line #replace me please# and #replace this please# some normal text here #replace me too# will turn into and some normal text here.