Tagged Questions
105
votes
5answers
62k 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 ...
92
votes
6answers
103k 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 ...
63
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 ...
54
votes
8answers
66k 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 ...
53
votes
6answers
8k views
How to determine if a number is a prime with regex?
I found the following code example for Java on RosettaCode:
public static boolean prime(int n) {
return !new String(new char[n]).matches(".?|(..+?)\\1+");
}
I don't know Java in particular but ...
52
votes
5answers
16k 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 ...
48
votes
11answers
15k 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"));
...
48
votes
9answers
40k 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 ...
47
votes
9answers
76k 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 ...
46
votes
5answers
2k 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 = ...
44
votes
5answers
19k 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 ...
43
votes
8answers
26k 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 ...
41
votes
9answers
28k 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 ...
41
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 ...
40
votes
9answers
96k 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 ...