Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am using java and have string which could have multiple spaces and equal to "=" sign as shown below.
String temp = "[name='FPC:CPU']/XM chip/allocate";
This temp string will passed to some other program which is failing because of space and equal sign.
How can i escape space and "=" character?
My desired out put from original string
[name='FPC:CPU']/XM chip/allocate
to
[name\='FPC:CPU']/XM\ chip/allocate
Wondering how can i do that using temp.replaceAll
That should be pretty straight forward.
System.out.println("foo bar=baz".replaceAll("([ =])" "\\\\$1"));
Should print this
foo\ bar\=baz
The parenthesis in the regular expression form a capturing group, and the character class [ =] will capture spaces and equal signs.
In the replace expression, the $1 refers to the first capturing group. The only thing that gets a bit tricky is escaping the backslash.
Normally, in a regular expression replacement the backslash itself is an escape character. So you'd need two of them together to insert a backslash, however backslash is also an escape in a Java String, so to put two backslashes into a Java String (to form the regular expression escape), you must insert four backslashes. So that's how you end up with "\\$1".
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I want to search into Java packages using the following expression:
com.company.*
Test example: https://regex101.com/r/tHTQd9/2
But when I use it into Java code it's not finding anything. Do I need to put some escape characters for .?
The following expression would work:
\bcom\.company\.\w[\w\.]*\b
Match between word-boundaries
Use literal dot characters by escaping
1 alphanumeric (or underscore) followed by 0 or more alphanumerics or dots
Pattern regex = Pattern.compile("\\bcom\\.company\\.\\w[\\w\\.]*\\b");
If you are looking for a word or more in the last sequence you can try:
com\\.company\\.\w+
Or, even more generic (any other character or more):
com\\.company\\..+
Please remember that this is quite generic and prone to errors.
If you provide a more detailed explanation or constraints we can help building a better RegEx.
Why double backslash in Java?
We know that the backslash character is an escape character in Java
String literals as well. Therefore, we need to double the backslash
character when using it to precede any character (including the \
character itself).
Source
In java to escape dot (.) you need to append double backslash (\\) so your regex will be like this:
com\\.company\\.*
Why double backslash is needed:
As dot(.) is a special symbol in regex so you need to escape it using a backslash (\) but as backslash also works as an escape character in java so it will be removed by java after processing the string. In order to preserve it, we need to add another backslash (\)
Regex string you will see
com\\.company\\.*
String after java processed it which will be the input as regex
com\.company\.*
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a string x.*y and it should be splitted as x and y.
So, how to do it ?
x.*y => [x,y]
Note: I need x,y into 2 different strings.
The code snippet is:
String[] splitString = "x.*y".split("\\.\\*");
System.out.println(Arrays.toString(splitString));
Output from this snippet is:
[x, y]
The split method of String takes a regular expression as argument. Since . and * have special meanings in regular expressions, we need to escape each with a backslash, \ . And since \ has a special meaning in a Java string literal, it too has to be escaped with \, making \\ each time.
Edit: Since all the escapes make the regular expression a pain to read, I actually recommend this variant:
String[] splitString = "x.*y".split(Pattern.quote(".*"));
The result is the same. The quote method returns a regular expression that matches the argument literally. In this case \Q.*\E is returned, where \Q and \E denote the begin and end of literal quotation.
I would suggest not using split, but making use of indexOf method to make subsstrings:
String stringToSplit="your.*string";
String s1=stringToSplit.substring(0,stringToSplit.indexOf('.'));
String s2=stringToSplit.substring(stringToSplit.indexOf('*')+1);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
In a Java app, I use this regex: (\w+)_\d to match patterns of this form:
apples_1
oranges_2
and then I use the first capturing group value (apples, oranges).
However, I now have a new request to also match these strings:
applesdrp_1
orangesdrp_2
where 'drp' is a fixed 3 character string, and the same values as before need to be captured: apples, oranges
So for example, if I use this regex: (\w+)(?:drp)?_\d
it will do the work on apples_1, but not for applesdrp_1.
Is there a way to do that with a regex?
You can use a non-greedy quantifier:
(\w+?)(?:drp)?_\d
In this way \w+? will take characters until it find "drp_N" or "_N" (where N is a digit).
If you use a greedy quantifier, \w+ takes all possible character (including the underscore and the digit since they are included in \w) and then gives back characters one by one until (?:drp)?_\d succeeds. But since (?:drp)? is optional, the regex engine stops to backrack when it find _N.
Yes, you can - one way would be using a negative lookbehind, to make sure, that the drp is forced outside the group, if it is present
(\w+)(?<!drp)(?:drp)?_\d+
See https://regex101.com/r/jJ1rM4/3 for a demo
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
My objective is to separate the numbers from the string, but in my array's first position i get a blank space. So i need help for that not to happen.
str1 = "Y=9x1+29x2";
String[] split2 = str2.split("[^-?.?0-9]+");
Blank space at the start is due to presence of non-digit character at the start of your input.
You can remove all non-digits at start before splitting:
String linha = "Y=9x1+29x2";
String[] split = linha.replaceFirst("[^-.\\d]+", "").split("[^-.\\d]+");
for (String tok: split2)
System.out.println(tok);
Output:
9
1
29
2
I think your question is rather vague, but after looking at it, I'm guessing that you want to extract the numbers out of the string, where a "number" has this format: an optional minus sign, followed by an optional decimal point, followed by one or more digits. I suspect you also want to include numbers that have digits followed by a decimal point followed by more digits.
I'm guessing this is what you want, because of the ? you put in your regex. The problem is that inside square brackets, ? doesn't mean "optional", and it doesn't mean "zero or one of something". It means a question mark. The regex [^-?.?0-9] means "match one character that is not a digit, a period, a hyphen, or a question mark". A pattern in square brackets always matches one character, and you tell it what characters are OK (or, if you begin with ^, what characters are not OK). This kind of "character set" pattern never matches a sequence of characters. It just looks at one character at a time. If you put + after the pattern, it still looks at one character at a time; it just does so repeatedly.
I think what you're trying to do is to take a pattern that represents a number, and then say "look for something that doesn't look like that pattern", and you tried to do it by using [^...]. That simply will not work.
In fact, split() is the wrong tool for this job. The purpose of split is to break up a string whose delimiters match a given pattern. Using it when the strings you want to keep in the array match a given pattern doesn't work very well, unless the pattern is extremely simple. I recommend that you create a Matcher and use the find() method in a loop. find() is set up so that it can find all matching substrings of a string if you call it repeatedly. This is what you want to accomplish, so it's the right tool.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Regular expression in java:
'String'.replaceAll("([aeioucgjkqsxyzbfpvwdtmn1234567890])\\1+", "$1")
Can someone explain what the different characters do?
Explanation:
[aeioucgjkqsxyzbfpvwdtmn1234567890] Matches a single character in the list.
([aeioucgjkqsxyzbfpvwdtmn1234567890]) Capturing group around the char class would capture that single character.
\1+ \1 is a pointer to refer the chars inside the group index 1. In our case, a single character is captured so it refers to that single character. \1+ means one or more occurrences of the characters inside group index 1.
For Example:
aaaa
The above regex would capture the first character and check if the following one or more characters are same as the first character which was captured. If yes, then the whole duplicated chars are replaced by a single char(which was inside group index 1 ), that is aaaa was replaced by a single a
DEMO
All letters that are listed between brackets will be replaced by $1 if after them comes a \1, which is a literal backslash one. The plus sign (+) means 1 or more.
Any sequence of 1 or more of the characters inside the brackets [...] will be replaced with $1.
For instance, this will remove all those characters from your string:
System.out.println(Str.replaceAll("([aeioucgjkqsxyzbfpvwdtmn1234567890])\1+", ""));