I just started learning java regex today so excuse me for taking a rather lazy approach to learning what's wrong with my regex. Basically I'm trying to split a string on 'white space', -, 'white space' pattern (where white space is either one or none) and I'm not sure why the pattern isn't compiling. I get an error on the second s in: [^(\s?-\s?)] (index 8). If someone could help me out, I'd really appreciate it!
Java regex: [^(\\s?-\\s?)]. PatternSyntaxException on 's'?
387 Views Asked by youngrrrr At
2
There are 2 best solutions below
Related Questions in JAVA
- Add image to JCheckBoxMenuItem
- How to access invisible Unordered List element with Selenium WebDriver using Java
- Inheritance in Java, apparent type vs actual type
- Java catch the ball Game
- Access objects variable & method by name
- GridBagLayout is displaying JTextField and JTextArea as short, vertical lines
- Perform a task each interval
- Compound classes stored in an array are not accessible in selenium java
- How to avoid concurrent access to a resource?
- Why does processing goes slower on implementing try catch block in java?
- Redirect inside java interceptor
- Push toolbar content below statusbar
- Animation in Java on top of JPanel
- JPA - How to query with a LIKE operator in combination with an AttributeConverter
- Java Assign a Value to an array cell
Related Questions in REGEX
- Check for numeric value with optional commas javascript
- CSV to XML XSLT: How to quote excape
- How can I determine the index of the same set of characters between two strings that are of different lengths?
- Max 3 digits, up to 3 decimals
- Regex for SQL insert query
- Javascript Regex to get specific string from two differently-formatted text blocks
- JavaScript differences beetween new Regex('regex', 'flags') and /regex/flags
- Java replace every Nth specific character (e.g. space) in String
- c# regex spain mobile phone
- Perl Regex: Merge multiple one-character substrings
- Using .css("background-color") for comparison jQuery/Js
- Unexpected NoReverseMatch error when using include() in urls patterns
- RegEx for all the javascript code except comments
- Regex: how to separate username:password?
- Customising a RegExp for international phone numbers
Related Questions in STRING
- SML - Find same elements in a string
- match hex string with list indice
- How can I determine the index of the same set of characters between two strings that are of different lengths?
- String.replace() isn't working like I expect
- How to do a case-insensitive string comparison?
- Trying to save an np array with string and floats, but getting a error
- String replace with integer not working
- How to calculate a length of array with out using library
- Java replace every Nth specific character (e.g. space) in String
- Split the strings into two parts Python
- Perl Regex: Merge multiple one-character substrings
- Squid S2275 does not know about format string argument indexes
- more efficient way of remove a few characters from the end of a string
- python member str performance too slow
- String.split() not behaving in android
Related Questions in PATTERNSYNTAXEXCEPTION
- Android: PatternSyntaxException: Syntax error U_ILLEGAL_ARGUMENT_ERROR
- Why is no PatternSyntaxException thrown for my invalid pattern?
- java.util.regex.PatternSyntaxException while using RowFilter in java swing
- Replace all method throws PatternSyntaxException
- Java regex: [^(\\s?-\\s?)]. PatternSyntaxException on 's'?
- how pattern syntax exception works
- Java: PatternSyntaxException thrown with regex .*-\\d+{.*}\\d+-.*
- Getting java.util.regex.PatternSyntaxException when using replaceAll method
- Android optional word boundary regex
- SyntaxException: line 2:10 no viable alternative at input 'UNIQUE' > (...NOT EXISTS books ( id [UUID] UNIQUE...)
- Does String.matches(<pattern>) throw anything?
- Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence near index 2
- Regex pattern SyntaxException
- What's wrong with my Groovy regex?
- Splitting a Java String on the "+" symbol gives an error
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
You are placing your pattern you are trying to split on inside of a negated character class in which it is doing the exact opposite of what you are expecting it to do.
Your syntax is indeed incorrect, but
why will it not compile?Well, Inside of a character class the hyphen has special meaning. You can place a hyphen as the first or last character of the class. In some regular expression implementations, you can also place directly after a range. If you place the hyphen anywhere else you need to escape it in order to add it to your class.To fix the compile issue, you would simply escape the hyphen, still this regex does not do what you want.
Remove the character class and the capturing group from the pattern:
Here are a few references for learning regular expressions.