Subtract from character class

Asked
Active3 hr before
Viewed126 times

6 Answers

characterclass
90%

... supports set operations on extended character classes as an experimental feature (available since Perl 5.18). In particular, you can directly subtract arbitrary character classes:,Note that each of these approaches is completely general in that you can subtract two arbitrarily complex character classes., Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers , How does the Bladesinging wizard's Extra Attack feature interact with the additional Attack action from the Haste spell?

... provides only one simple character class set operation: subtraction. This is enough for your example, so you can simply use

[\w - [_]]

... provides a much richer set of character class set operations. In particular you can get the intersection of two sets like [[abc]&&[cde]] (which would give c in this case). Intersection and negation together give you subtraction:

[\w && [ ^ _]]

... supports set operations on extended character classes as an experimental feature (available since Perl 5.18). In particular, you can directly subtract arbitrary character classes:

( ? [\w - [_]])

... (that support lookaheads) allow you to mimic the subtraction by using a negative lookahead:

( ? !_)\ w
load more v
88%

You can use subtraction to negate one or more nested character classes. This example creates a single character class that matches everything from a to z, except the vowels (‘a’, ‘i’, ‘u’, ‘e’, ‘o’). This can be written in a subtraction pattern as [a-z&&[^aiueo]].,This site uses Akismet to reduce spam. Learn how your comment data is processed.,Learn Java by Examples,Here are the result of the program:

You can use subtraction to negate one or more nested character classes. This example creates a single character class that matches everything from a to z, except the vowels (‘a’, ‘i’, ‘u’, ‘e’, ‘o’). This can be written in a subtraction pattern as [a-z&&[^aiueo]].

package org.kodejava.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CharacterClassSubtractionDemo {
   public static void main(String[] args) {
      // Define regex that will search characters from 'a' to 'z'
      // and excluding vowels.
      String regex = "[a-z&&[^aiueo]]";

      // Compiles the given regular expression into a pattern and
      // Creates a matcher that will match the given input against
      // this pattern.
      Pattern pattern = Pattern.compile(regex);
      Matcher matcher = pattern.matcher("The quick brown fox.");

      // Find every match and print it
      while (matcher.find()) {
         System.out.format("Text \"%s\" found at %d to %d.%n",
            matcher.group(), matcher.start(), matcher.end());
      }
   }
}
load more v
72%

load more v
65%

If you browse through the Pattern class specification, you'll see tables summarizing the supported regular expression constructs. In the "Character Classes" section you'll find the following:,Now that we've covered how character classes are created, You may want to review the Character Classes table before continuing with the next section.,And here's an example that shows the intersection of two ranges:,The match is successful only if the first character of the input string does not contain any of the characters defined by the character class.

 Enter your regex: [bcr] at
 Enter input string to search: bat
 I found the text "bat"
 starting at index 0 and ending at index 3.

 Enter your regex: [bcr] at
 Enter input string to search: cat
 I found the text "cat"
 starting at index 0 and ending at index 3.

 Enter your regex: [bcr] at
 Enter input string to search: rat
 I found the text "rat"
 starting at index 0 and ending at index 3.

 Enter your regex: [bcr] at
 Enter input string to search: hat
 No match found.
load more v
75%

Character subtraction expressions have the following form:,Implement (addition and subtraction) calculator using regular expressions (C # implementation),Square brackets ([]) and hyphen (-) is mandatory. base_group is a positive character group or a negative character group. ToThe excluded_group part is another positive character group or negative character group, or another character subtraction expression (that is, character subtraction expressions can be nested).,The following example defines a regular expression^[0-9-[2468]]+$, this expression matches zero and odd numbers in the input string. The regular expression pattern can be interpreted as shown in the following table.

 using System;
 using System.Text.RegularExpressions;

 public class Example {
    public static void Main() {
       string[] inputs = {
          "123",
          "13579753",
          "3557798",
          "335599901"
       };
       string pattern = @ "^[0-9-[2468]]+$";

       foreach(string input in inputs) {
          Match match = Regex.Match(input, pattern);
          if (match.Success)
             Console.WriteLine(match.Value);
       }
    }
 }
 // The example displays the following output:
 //       13579753
 //       335599901
40%

I'm using a specific pattern in attempt to get a sentence without the punctuation. I have this:,MSDN - Regular Expression Character Classes - Subtraction,What is the point behind character class intersections in Java’s Regex?,The most important "caveat" of Java regex is that matches attempts to match a pattern against the whole string. This is atypical of most engines, and can be a source of confusion at times.

I'm using a specific pattern in attempt to get a sentence without the punctuation. I have this:

 p = Pattern.compile("[\p{Punct}\s]+");
 String[] all = p.split("int a = 4;");

I get the array

all = {
   int,
   a,
   4
};

That is great but I want my pattern to also keep "[]" if it exists. for example:

String[] all = p.split("int[] a = 4;");

and get

all = {
   int,
   [],
   a,
   4
};
load more v

Other "character-class" queries related to "Subtract from character class"