What is the equivalent of square brackets for multiple characters? [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Regex [abcd] can take any one character out of a, b, c and d.
What if I want to take any one string out of abc, def and ijk.
So something like ["abcd" "def" "efg"] (but this obviously doesn't work).
How would I do this in Java?

your regex could look like this:
(abcd|def|efg)

[] is only for single characters.
You can use | for multiple characters (X|Y means "Either X or Y"):
abcd|def|efg
In case there's anything else in the regex, you'd want to surround it with brackets:
other(abcd|def|efg)stuff
The above matches these strings:
otherabcdstuff
otherdefstuff
otherefgstuff
Where-as:
otherabcd|def|efgstuff
would obviously match these strings:
otherabcd
def
efgstuff

Related

Regular Expression to parse category steps [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this String
English>Arts>Photography
Want regular expression to delete all after last (>) ,Want output to be like this
English>Arts>
Many Thanks,
use String.lastIndexOf() instead of String.indexOf(). no need to go to regexps for this
Use LastIndexOf as mentioned below :
Substring(0, [YourString].LastIndexOf('>'));
You can use substring and lastIndexOf as shown below:
String s = "English>Arts>Photography";
System.out.println( s.substring(0,s.lastIndexOf('>') ));
If you really didn't want to use .lastIndexOf() (i don't know why you wouldn't). The regex would have been:
String input = "English>Arts>Photography";
Pattern p = Pattern.compile("(.*>)[A-Za-z]+");
Matcher m = p.mathcer(input);
if (m.matches()) {
String output = m.group(1);
}

regex (Regular Expression )for sub string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am having an activity that return string message
String value of my file id like
file!^#123456
I am interesting only the number of that file id i see some regex expression but i did not understand properly.
if I get it right regex for "I am interesting only the number of that file id" will be : [0-9]+ or with Java Predefined Character Classes; [\d]+.
Try this.
String str = "file!^#123456";
System.out.println(str.replaceAll("\\D+",""));
You could go at it using substring on it's own:
String s = "file!^#123456";
System.out.println(s.substring(s.indexOf("#") + 1, s.length()));

Keep Single dashes and single space in string - Negative Lookahead [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm looking for a java regex pattern that exclude any text that have two or more consecutive dashes or two or more consecutive spaces.
Assuming that you will use your regex in mechanism similar to matches method you may be looking for something like
^(?!.*(--| )).*$
try this
s = s.replaceAll("( )+|(-)+", "$1$2");

How to write a regular expression to remove all alphaberical characters from a String [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to remove all alphabetical characters from a string usign a regular expression in java/android?
val = val.replaceAll("/A/z","");
Try this:
replaceAll("[a-z]", "");
Also have a look here:
Replace all characters not in range (Java String)
This will remove all alphabetical characters
String text = "gdgddfgdfh123.0114cc";
String numOnly = text.replaceAll("\\p{Alpha}","");
Have a look into Unicode properites:
\p{L} any kind of letter from any language
So your regex would look like this
val = val.replaceAll("\\p{L}+","");
To remove also combined letters use a character class and add \p{M}
\p{M} a character intended to be combined with another character (e.g. accents, umlauts, enclosing boxes, etc.)
Then you end here:
val = val.replaceAll("[\\p{L}\\p{M}]+","");

Regular expression for lines starts with capital letter word [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have below lines of text in my text file,
James is working in London
this is a program developed in java
Program is working
I want to get the lines which have starting word with capital letter
James is working in London
Program is working
Thanks
For English language, you can use this
^[A-Z].*
The ^ is for start of line. and [A-Z] means any capital letter.
More faster if you use:
if (Character.isUpperCase(line.charAt(0)) {
System.out.println(line);
}

Categories