Skip to content

Tag: regex

How to grab all words that start with capital letters?

I want to create a Java regular expression to grab all words that start with a capital letter then capital or small letters, but those letters may contain accents. Examples : Where Àdónde Rápido Àste Can you please help me with that ? Answer Regex: Java string: Explanation: Caveat: This only works correctly i…

Regular expression to match unescaped special characters only

I’m trying to come up with a regular expression that can match only characters not preceded by a special escape sequence in a string. For instance, in the string Is ? stranded//? , I want to be able to replace the ? which hasn’t been escaped with another string, so I can have this result : **Is Da…

Regular expression with variable number of groups?

Is it possible to create a regular expression with a variable number of groups? After running this for instance… … I would like to have something like m.group(1) = “c” m.group(2) = “d” m.group(3) = “d” m.group(4) = “c”. (Background: I’m parsing…

How to filter string for unwanted characters using regex?

Basically , I am wondering if there is a handy class or method to filter a String for unwanted characters. The output of the method should be the ‘cleaned’ String. Ie: Expecting result would be: A better example: I expect the result to be: Because, i let the cleaner know that ‘ ‘, &#82…

Removing all whitespace characters except for ” “

I consider myself pretty good with Regular Expressions, but this one is appearing to be surprisingly tricky: I want to trim all whitespace, except the space character: ‘ ‘. In Java, the RegEx I have tried is: [s-[ ]], but this one also strips out ‘ ‘. UPDATE: Here is the particular str…

Remove all empty lines

I thought that wasn’t that hard to do, but I want to remove all empty lines (or lines just containing blanks and tabs in Java) with String.replaceAll. My regex looks like this: But it doesn’t work. I looked around, but only found regexes for removing empty lines without blanks or tabs. Answer Try …

Search for a word in a String

If I am looking for a particular word inside a string, for example, in the string “how are you” I am looking for “are”. Would a regular indexOf() work faster and better or a Regex match() Which of the two methods above is a better way of looking for a string inside another string? Or i…