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 8 years ago.
Improve this question
I have a list of strings, like c++, c, java, c#, .net.
I have to find the occurrences of these strings in some text.
I tried,
String pattern = "(?i)\\b"+Pattern.quote(str)+"\\b";
But it doesn't match with c++.
Then, I removed \b and it started matching every c in the text.
How do I match the whole word?
Sample String:
C, c#, C++ college cat cow
\bc\+\+\b cannot c++ because + is not considered a word character. \b can only match after a word character not after a non-word character like +.
You can probably use this regex:
\bc\+\+(?=\W|$)
Regex Demo
Related
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 3 months ago.
Improve this question
So I need to check if a word is a pattern of alternating vowel and cosonant (Or consonant and vowel) in Java.
I want to make it a regex but I just came with this incomplete regex expression:
[aeiouAEIOI][^aeiouAEIOI]
Any ideas?
Thanks :)
Update: It's not regex restricted, so it can be an option if anyone has any ideas
One way is using a lookahead to check if neither two vowels nor two consonants next to each other.
(?i)^(?!.*?(?:[aeiou]{2}|[^aeiou]{2}))[a-z]+$
See this demo at regex101 (used i flag for caseless matching, the \n in demo is for staying in line)
Update: Thank you for the comment #Thefourthbird. For matching at least two characters you will need to change the last quantifier: Use [a-z]{2,} (two or more) instead of [a-z]+ (one or more). For only matching an even amount of characters (2,4,6,8...), change this part to: (?:[a-z]{2})+
FYI: If you use this with matches you can drop the ^ start and $ end anchor (see this Java demo).
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 4 years ago.
Improve this question
How do I can make regular expression, that accept all alphanumeric, except ,(coma).I tried the following expression [^,]{0,10}.but the hyphen is not counting in length.. Pls suggest me. My input is "12345-7890" and "1a2s2-6s7a"
This regex will exclude any pattern that contains a comma.
^((?!\-)[^\,]{0,10}|(?=\-)[^\,]{0,9})$
The regex will detect any hyphen and set the maximum length to 9 (if not contains hyphen) or 10 otherwise. So the hyphen still counts to length, which is adjusted dynamically. This only works with one hyphen.
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".
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 5 years ago.
Improve this question
I need to match any letters (like MS Office Word find with special character ^$ functionality) with regex.
I've tried with [a-zA-Z] but don't match any unicode letters like accent letters or ä, ö, ü, ß.
I've tried also with [a-zA-ZäöüßÄÖÜ]but there are too many letters.
Is there any regex to match all this letters?
This \\p{L} regex would match any kind of letter from any language.
DEMO
To match any unicode letter in Java use:
\\p{L}
You can use \\p{L} to match any letter, Unicode included.
For fine-tuned matching, you can consult the documentation on filefront, and combine it with the Unicode features documented in Java Pattern here.
Quick example
String input = "ZäöüßÄÖÜß您好";
System.out.println(input.matches(String.format("\\p{L}{%d}", input.length())));
Output
true
It seems you want to match not any letter (eg Arabic characters), but Latin characters:
\p{IsLatin}+
Using your chars:
System.out.println("ZäöüßÄÖÜ".matches("\\p{IsLatin}+")); // true