Eclipse regex for finding the following string - java

I want to search all files in my project in eclipse and replace all occurences of ConnectionID="lettersandnumbers"
what is the regex for the bit between the quotation marks(including the quotations)?

assuming you want to replace the String contents, look for
(ConnectionID=").*?(")
and replace with
$1replacement$2
where replacement is what you want to replace the String with :-)

Put this in your search box:
ConnectionID=\"[\w]+\"

Try this:
ConnectionID="[\p{L}\p{N}]*"
This will match all Unicode letters and numbers. Remember, à is a letter too.

I would search for:
ConnectionID="(.*)"
If you're not interested in keeping the chars between quotes you can use
ConnectionID=".*"

Related

How to eliminate the special character ^\^# from String in Java

My application is reading a file which contains following data:
MENS HEALTH^\^# P
while actual text should be
MENS HEALTH P
I have already replaced the '\u0000' but still "^\" is still remaining in the string. I am not sure what is code for this characters, so I can replace it.
When I open the file in intelliJ editor it's displayed as FS symbol.
Please suggest how I can eliminate this.
Thanks,
Rather than worry about what characters the junk consists of, remove everything that isn't what you want to keep:
str = str.replaceAll("[^\\w ]+", "");
This deletes any characters that are not word characters or spaces.
You can use a regular expression with String.replaceAll() to replace these characters.
Note that backslash has a special meaning and need to be escaped (with a backslash).
"my\\^#String".replaceAll("[\\\\^#]", "");
Online Demo

How can I index a specific word in a String in Java?

I would like to replace all occurrences of a specific word in a Java String. For instance, I would like replace all cat in following string by cat. I have tried to implementing regex by setting pattern as \\bcat\\b, but it seems not working for the case when non-alphanumeric characters like underscore are around it.
cat catabolic_cat_4cat,6cat
How can I make a regex to exclude all non-alphanumeric characters around the word and output a sting like this:
c*t catabolic_c*t_4cat,6cat
You could just alternate the word boundary character class with a class of your own making like so, and (as per Andreas' suggestion) use lookarounds to prevent catching the surrounding characters in the replace.
(?<=^|[^a-zA-Z0-9])cat(?=[^a-zA-Z0-9]|$)
How about make your code cleaner by using this inbuilt String function:
String.replaceAll("oldText","newText");

Using Regexp in Java to remove some text

It is maybe a simple question. But I tried a lot of Regexp combinations and still not worinkg. My problem is: I have words like: Test=move or Testing=move
I would like to remove the text 'Test=' or 'Testing='. In other words i need only the 'move' text after the '='. What is the best way to do that in Java? Thanks.
I think that for this problem, the split(string regex) is better suited:
String str = "Test=move";
System.out.println(str.split("=")[1]);
I would replace \w+= with "" - this will get rid of any work preceding an equals sign.
myString.replaceAll("\w+=", "");
If the string before the equals sign has more than just letters you can add them to an optional selection:
myString.replaceAll("[\w-\.\d]+=", "");
This will remove any word with letters, numbers, hyphens and periods.

Java Regex - Finding specific string within a String

I am trying to match a string that start with the set word "hotel", then a hyphen, then a word of any length, then another hyphen and finally a number of any length.
Edit: Dima gave the solution I needed in the comments of this question! Thanks Dima.
Further edit: elaborating on Dima's answer, adding capturing groups making it easier to retrieve the information entered, and correcting the last bit to only accept digits:
^hotel-(.+)-(\d+)
^hotel-(.)*$
(But hotel-something WILL work, according to your initial statement).
So, if you actually want something like:
hotel-XXXXXX-YYYYYYY
Then the regex is :
^hotel-(.)*-(.)*$
Try a regex online tester like http://www.regextester.com/.
If you want to match the start of the input, you use ^.
so if you have ^hotel-\b, that will force hotel to be at the start of the string.
as a note, you can use $ for the end of the string in a similar way.
\bhotel-[^\s-]+-[^\s-]+\b
\b means that it should be a word boundery
[^\s-] means anything but - or whitespace
https://regex101.com/r/mH3vY8/1

How can I add an underscore before each capital letter inside a Java String?

I have a string like this "HelloWorldMyNameIsCarl" and I want it to become something like "Hello_World_My_Name_Is_Carl". How can I do this?
Yes, regular expressions can do that for you:
"HelloWorldMyNameIsCarl".replaceAll("(.)([A-Z])", "$1_$2")
The expression [A-Z] will match every upper case letter and put it into the second group. You need the first group . to avoid replacing the first 'H'.
As Piligrim pointed out, this solution does not work for arbitrary languages. To catch any uppercase letter defined by the Unicode stardard we need the Unicode 4.1 subproperty \p{Lu} which matches all uppercase letters. So the more general solution looks like
"HelloWorldMyNameIsCarl".replaceAll("(.)(\\p{Lu})", "$1_$2")
Thanks Piligrim.
Is this homework? To get you started:
Create a StringBuffer
Iterate over your string.
Check each character to be uppercase (java.lang.Character class will help)
Append underscore to buffer if so.
Append current character to buffer.
Here's a hint to get you thinking along a possible solution:
Find a way of splitting the string into parts at each capital letter
Join the split strings back up with underscores between them
Useful keywords:
split
regular expression/regex

Categories