74799

How can i write a more complex regexp in java?

Question:

id like to do the following rules:

Not begining with space " ", not more than one "." , not begining with "." not end with "." not ? / \ : ; in the file.

public static void Invalid(String[] filename){ for(String s: filename){ String u = s; try { u = new String(s.getBytes(), "UTF-8"); System.out.println(u); } catch (Exception e) { e.printStackTrace(); } u=u.replaceAll("[.$]", " ");//.{2,}\\? u = u.replaceAll("\\s+", "_"); System.out.println(s + " = " + u); } }

in main just simple:

String[] name={"some?thing..txt."}; Invalid(name);

output:

some?thing..txt. some?thing..txt. = some_thing_txt_

how can i do that?

Answer1:

You can use this pattern to validate the filename:

^[^?/\\\\;: .][^?/\\\\;:.]*(?:\\.[^?/\\\\;:.]+)?$

Note: if you want to allow the empty string, add |^$ at the end.

Not sure what you exactly want to do with invalid filenames.

Recommend

  • CLR Stored Procedure with C# throwing errors
  • What would this code do? (memory management)
  • iterator_range in header file
  • In regards to the difference between Java “Properties” and C#' Properties
  • Send the string to its hex equivalent
  • Xamarin PCLCrypto SHA256 give different hash
  • Cross platform UI spacing/padding
  • How can I include If-None-Match header in HttpRequestMessage
  • Django model inheritance, filtering models
  • Reading a file into a multidimensional array
  • CakePHP ACL tutorial initDB function warnings
  • how to upload multiple files in c# windows application
  • App restarts from wrong activity
  • Android - Material Design - NavigationView - How to put vertical scroll?
  • Converting a WriteableBitmap image ToArray in UWP
  • Jquery UI tool tip close icon
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Jetty Server not starting: Unable to establish loopback connection
  • How to install a .deb file on a jailbroken iphone programmatically?
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • Handling un-mapped Rest path
  • Using variable in a value field in jMeter
  • Why value captured by reference in lambda is broken? [duplicate]
  • Splitting given String into two variables - php
  • Encrypt data by using a public key in c# and decrypt data by using a private key in php
  • javascript inside java/jsp code
  • SSO with signing and signature validation doesn't work
  • Validaiting emails with Net.Mail MailAddress
  • sending/ receiving email in Java
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • 0x202A in filename: Why?
  • Numpy divide by zero. Why?
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • How does Linux kernel interrupt the application?
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++
  • reshape alternating columns in less time and using less memory
  • costura.fody for a dll that references another dll