Regex Question - exclude certain characters

If I want to exclude / * in regex, how would I write it?

I tried:

[^/[*]]
([^/][^*])

But to no avail ...

+3
source share
5 answers

You must match not /, OR / followed by not *:

([^/]|/[^*])+/?
+3
source

Use a negative lookahead to verify that if the input contains /, it does not contain /, followed by /*. In JavaScript , xwhich should not be followed , therefore y, will x(?!y):

  • Either the input does not contain /:, new RegExp('^[^/]*$')or
  • / *: new RegExp('^/(?!\\*)$') ( , * \\, * .

:

new RegExp('^([^/]|/(?!\\*))*$')
+2

Java :

Pattern pt = Pattern.compile("((?<!/)[*])");

*, /.

String str = "Hello /* World";
Pattern pt = Pattern.compile("((?<!/)[*])");
Matcher m = pt.matcher(str);
if (m.find()) {
    System.out.println("Matched1: " + m.group(0));
}
String str1 = "Hello * World";
m = pt.matcher(str1);
if (m.find()) {
    System.out.println("Matched2: " + m.group(0));
}

OUTPUT

Matched2: *
0

? , ​​ "/*", * , , -, . * /\\* : ".*/\\*.*",

val samples = List ("*no", "/*yes", "yes/*yes", "yes/*", "no/", "yes/*yes/*yes", "n/o*")  
samples: List[java.lang.String] = List(*no, /*yes, yes/*yes, yes/*, no/, yes/*yes/*yes, n/o*)

scala> samples.foreach (s => println (s + "\t" + (! s.matches (".*/\\*.*"))))                   
*no true
/*yes   false
yes/*yes    false
yes/*   false
no/ true
yes/*yes/*yes   false
n/o*    true
0

. http://mindprod.com/jgloss/regex.html#EXCLUSION

eg. [^ "] or [^ wz] for anything but a quote and nothing but wxyz

-1
source

All Articles