Tagged Questions
99
votes
5answers
57k views
How to escape text for regular expression in Java
Does Java have a built-in way to escape arbitrary text so that it can be included in a regular expression? For example, if my users enter "$5", I'd like to match that exactly rather than a "5" after ...
84
votes
6answers
95k views
Split Java String by New Line
I'm trying to split text in a JTextArea using a regex to split the String by \n However, this does not work and I also tried by \r\n|\r|n and many other combination of regexes.
Code:
public void ...
62
votes
2answers
1k views
Where can I find unit tests for regular expressions in multiple languages?
I'm building a regex helper at http://www.debuggex.com. The amount of detail I want to show requires me to write my own parser and matcher.
To make sure my parser and matcher work correctly, I've ...
52
votes
8answers
61k views
Java - escape string to prevent SQL injection
I'm trying to put some anti sql injection in place in java and am finding it very difficult to work with the the "replaceAll" string function. Ultimately I need a function that will convert any ...
47
votes
5answers
14k views
Unicode equivalents for \w and \b in Java regular expressions?
Many modern regex implementations interpret the \w character class shorthand as "any letter, digit, or connecting punctuation" (usually: underscore). That way, a regex like \w+ matches words like ...
44
votes
10answers
14k views
How do I convert CamelCase into human-readable names in Java?
I'd like to write a method that converts CamelCase into a human-readable name.
Here's the test case:
public void testSplitCamelCase() {
assertEquals("lowercase", splitCamelCase("lowercase"));
...
43
votes
9answers
35k views
Java: splitting a comma-separated string but ignoring commas in quotes
I have a string vaguely like this:
foo,bar,c;qual="baz,blurb",d;junk="quux,syzygy"
that I want to split by commas -- but I need to ignore commas in quotes. How can I do this? Seems like a regexp ...
43
votes
9answers
69k views
Using Regular Expressions to Extract a Value in Java
I have several strings in the rough form:
[some text] [some number] [some more text]
I want to extract the text in [some number] using the Java Regex classes.
I know roughly what regular ...
41
votes
5answers
18k views
Regular Expressions and GWT
My questions is: Is there a good solution to use regular expression in GWT?
I'm not satisfied with the use of String.split(regex) for example. GWT translates the Code to JS and then uses the regex as ...
39
votes
9answers
27k views
Regex for splitting a string using space when not surrounded by single or double quotes
I'm new to regular expressions and would appreciate your help. I'm trying to put together an expression that will split the example string using all spaces that are not surrounded by single or double ...
39
votes
9answers
2k views
Why are most string manipulations in Java based on regexp?
In Java there are a bunch of methods that all have to do with manipulating Strings.
The simplest example is the String.split("something") method.
Now the actual definition of many of those methods is ...
38
votes
3answers
3k views
How can we match a^n b^n with Java regex?
This is the second part of a series of educational regex articles. It shows how lookaheads and nested references can be used to match the non-regular languge anbn. Nested references are first ...
38
votes
5answers
1k views
Extracting pairs of words using String.split()
Given a String such as
String input = "one two three four five six seven";
Is there a regex that works with String.split() to grab (up to) two words at a time, such that:
String[] pairs = ...
36
votes
9answers
89k views
Using Java to find substring of a bigger string using Regular Expression
If I have a string like this:
FOO[BAR]
I need a generic way to get the "BAR" string out of the string so that no matter what string is between the square brackets it would be able to get the ...
36
votes
7answers
23k views
What is the simplest way to convert a Java string from all caps (words separated by underscores) to CamelCase (no word separators)?
The title pretty much says it all. What's the simplest/most elegant way that I can convert, in Java, a string from the format "THIS_IS_AN_EXAMPLE_STRING" to the format "ThisIsAnExampleString"? I ...