Regex to remove anything that's not numeric or specific words

regex match everything except pattern
how to remove parentheses in regex
regex replace everything except
regex remove brackets
regex remove word from string
regex remove pattern
notepad++ delete everything except regex
regex all except word

So I have a mathematical algorithm creator where the user inserts variables from a list of possible variables and they can create an algorithm with only those variables and numbers and characters used in math like parenthesis and others. I need a regular expression that will remove from the string anything that is not either one of these variables or ().*-+/ or a number. I've tried many regular expressions but none have done what I need all the variables must be surrounded by curly brackets.

For example

/^(?!{profitPercent}|{productPrice}|{weight}|{quantityInCart}|{lineTotal}|{cartTotal}|{billingUnits}|{test_111}|{test_213}|{test_prod_input_15}|{test_prod_input_16}).*/g

This just removes everything, and it doesn't have the exception for numbers and ().*-+/

Say I have the following string:

var str='this is a string with a {productPrice} variable and some 827/100 math in it';

I need to remove everything except

{productPrice}827/100

This is my code:

var reg=new RegExp("^(?!{" + escapeString(variableNames.join("}|{")) + "}).*","g");
txt=txt.replace(reg,'');

I've tried many many different expressions, none of them worked.

In stead of asserting the start of the string ^ and using the variables in a negative lookahead (?! you can use them in a non capturing group (?:

Then add the variables you want to match to a character class [().*+/\d]+ and add that to the alternation.

This will give you the matches that you want to keep, and you can use join using an empty string as the delimiter to concatenate them, resulting in:

{productPrice}827/100

Your code (you have to add escapeString) could look like:

var variableNames = [
  "profitPercent",
  "productPrice",
  "weight",
  "quantityInCart",
  "lineTotal",
  "cartTotal",
  "billingUnits",
  "test_111",
  "test_213",
  "test_prod_input_15",
  "test_prod_input_16"
];

var str = 'this is a string with a {productPrice} variable and some 827/100 math in it';
var reg = new RegExp("(?:{" + variableNames.join("}|{") + "}|[\\d)(.*+/]+)", "g");
str = str.match(reg).join('');
console.log(str);

Regex for remove everything after | (with, The pipe, | , is a special-character in regex (meaning "or") and you'll have to escape it with a \ . Using your current regex: \|.*$. I've tried this in Notepad++,  I'm using a Google Sheets and trying to create a formula to remove a period and anything that comes before it in a cell. For example: idontwantthis.iwantthis I have used this in the past: =(RIGHT(D2,FIND("whatever characters here",D2,1) - 1)) but the period "." is a wildcard. I have googled around for a few hours and still have nothing that works.

Remove everything from text except what is in brackets with Regex , RegEx for Remove everything from text except what is in brackets with Regex. Test String. [D-1.1.1-A] - bla bla text here[D-1.1.1-B] - some more bla bla text  In a regex, a period character by itself is used to symbolize a wildcard; as in, it can represent any character. By using a slash, "\", you tell the regex you want to match exactly the period character. This slash is very important here!! \w+ : One or more word characters. \w matches any alphabetical character as well as any digit.

Might be easier if you split up the string first i.e.

const whitelist = new Set([
  '{profitPercent}',
  '{productPrice}',
  '{weight}',
  '{quantityInCart}',
  '{lineTotal}',
  '{cartTotal}',
  '{billingUnits}',
  '{test_111}',
  '{test_213}',
  '{test_prod_input_15}',
  '{test_prod_input_16}',
])

const regex = /[0-9.*\-+\/]/;
const str = 'this is a string with a {productPrice} variable and some 827/100 math in it'

const newStr = str.split(' ').filter((word) => {
  return whitelist.has(word) || regex.test(word)
}).join('')

console.log(newStr)

Solved: How to use RegEx to remove text in a string and re , Using the RegEx tool the Regular Expression would be (.*)T(.*)\..* with the output method as Replace and the Replacement Text set to $1 $2. regex_replace([Input Date],"(.*)T(.*)\..*",'$1 $2') The first formula takes the left 19 characters and then replaces the T with a space. The second formula takes everything up to the T and everything between the T and the Period and puts a space between those groups.

Can be done like this.

/.*?(?:((?:{profitPercent}|{productPrice}|{weight}|{quantityInCart}|{lineTotal}|{cartTotal}|{billingUnits}|{test_111}|{test_213}|{test_prod_input_15}|{test_prod_input_16}|[-().*+\d\/])+)|$)/g

https://regex101.com/r/lHx4FE/1

Instead of deleting what you DON'T want, delete all but what you DO want. The positive interpretation is always better.

Replace with $1 the stuff you want.

Expanded

 .*? 
 (?:
      (                             # (1 start)
           (?:
                {profitPercent}
             |  {productPrice}
             |  {weight}
             |  {quantityInCart}
             |  {lineTotal}
             |  {cartTotal}
             |  {billingUnits}
             |  {test_111}
             |  {test_213}
             |  {test_prod_input_15}
             |  {test_prod_input_16}
             |  [-().*+\d/] 
           )+
      )                             # (1 end)
   |  
      $ 
 )

What is the regular expression to remove the string "abc\" from a , Or any string where the fourth character is something other than a letter or a digit, since that's what \W means. This might be what you want, but  As mentioned, this is not something regex is “good” at (or should do), but still, it is possible. And if you need to match line break chars as well, use the DOT-ALL modifier (the trailing s in the following pattern):

Remove everything from text except what is in , Regular Expression to Remove everything from text except what is in brackets with Regex. [D-1.1.1-A] - bla bla text here[D-1.1.1-B] - some more bla bla text  Regular Expression to . Character classes. any character except newline \w \d \s: word, digit, whitespace

Solved: Magic Replace Text REGex, remove anything outside , What you're asking for can be more complicated than a regex if the input is more complex. However if the input is always as written, then. Ctrl-H Find what:  The tables are meant to serve as an accelerated regex course, and they are meant to be read slowly, one line at a time. On each line, in the leftmost column, you will find a new element of regex syntax. The next column, "Legend", explains what the element means (or encodes) in the regex syntax.

Removing all digits, non-digits, whitespaces (from Strings) and other , Solved: Hello! I am trying to use the regex replace text in magic and am having difficulty getting the desired result. I need to capture a string. If you prefer not to use REGEX functionality then this can be solved another way. Use the # character as a token for splitting the address field using the TEXT TO COLUMNS tool. Taking this one step further you can convert embedded text strings with the equivalent meaning (eg UNIT, APT, SUITE etc) to the # character using the FIND AND REPLACE TOOL.

Comments
  • thanks this worked. i'm now trying to apply the same regex in php using preg_replace to do the same thing as this. i'm using preg_replace('/(?:{'.implode('}|{',array_column($vars,'name')).'}|[\\d0-9'.preg_quote(implode('',$chars)).'\/]+)/','',$algo); but it's returning the text that i do not want. what am i doing wrong here?
  • @SuzanneEdelman You are using preg_replace but I think you could use preg_match_all, for example 3v4l.org/9vfD7