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);
}
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I need to identify a pattern in given text (string), and I'm looking for a regex for the same. Using a Regex is preferable due to the framework I'm working in.
For instance, consider the text --
Problem:
<<< empty line(s) >>>>
Reason:
here goes some multi-line reasoning...
...
...
As you can see there is "no text (empty line(s)) after Problem: and before Reason: ".
I need to be able to identify this pattern from the text given to me, using a regex.
Any help is much appreciated.
Thanks!
The simplest regex would be
Pattern regex = Pattern.compile("Problem:\\s+Reason:");
which finds the text Problem:, followed by one or more whitespace characters, followed by the text Reason:.
If you want to make sure that there are at least two linebreaks between the two texts, you could also do
Pattern regex = Pattern.compile("Problem:[ \t]*\r?\n[ \t]*\r?\nReason:");
but that's probably not necessary.
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()));
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");
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
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}]+","");