Tagged Questions
47
votes
4answers
11k 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 ...
40
votes
9answers
32k 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 ...
48
votes
8answers
55k 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 ...
93
votes
5answers
51k 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 ...
28
votes
12answers
23k views
Is there a way to split strings with String.split() and include the delimiters?
I'm trying to split a string with all non-alphanumeric characters as delimiters yet Java's String.split() method discards the delimiter characters from the resulting array. Is there a way to split a ...
6
votes
7answers
34k views
How to use regular expressions to parse HTML in Java?
Please can someone tell me a simple way to find href and src tags in an html file using regular expressions in Java?
And then, how do I get the URL associated with the tag?
Thanks for any suggestion.
...
42
votes
10answers
12k 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"));
...
35
votes
9answers
25k 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 ...
1
vote
7answers
32k views
Parsing XML with REGEX in Java
Given the below XML snippet I need to get a list of name/value pairs for each child under DataElements. XPath or an XML parser cannot be used for reasons beyond my control so I am using regex.
...
35
votes
9answers
11k views
Using Regex to generate Strings rather than match them
I am writing a Java utility which helps me to generate loads of data for performance testing. It would be really cool to be able to specify a regex for Strings so that my generator spits out things ...
11
votes
4answers
2k views
Java split is eating my characters
I have a string like this String str = "la$le\\$li$lo".
I want to split it to get the following output "la","le\\$li","lo". The \$ is a $ escaped so it should be left in the output.
But when I do ...
22
votes
8answers
19k views
Split string to equal length substrings in Java
How to split the string "Thequickbrownfoxjumps" to substrings of equal size in Java.
Eg. "Thequickbrownfoxjumps" of 4 equal size should give the output.
["Theq","uick","brow","nfox","jump","s"]
...
27
votes
1answer
506 views
String.replaceAll() anomaly with greedy quantifiers in regex
Can anyone tell me why
System.out.println("test".replaceAll(".*", "a"));
Results in
aa
Note that the following has the same result:
System.out.println("test".replaceAll(".*$", "a"));
I have ...
33
votes
6answers
19k views
Regex Named Groups in Java
It is my understanding that the java.regex package does not have support for named groups (http://www.regular-expressions.info/named.html) so can anyone point me towards a third-party library that ...
39
votes
9answers
63k 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 ...