Reputation: 181
String str = "Text0TEXT1.more text ";
String str = "Text0TEXT1(more text ";
String str = "Text0TEXT1{more text ";
If I have a line that it end could be several chars such as . or ( or { or ; how can I extract TEXT1 only ?
Update: There is Text 0 before text 1 and the special char may or may not exist
Update 2
String str = "Beginning text Text I want . Text I don't want"
String str = "Beginning text with numbers Text I want ( Text I don't want )"
String str = "Beginning text with numbers Text I want { Text I don't want }"
I need to extract "Text I want" but I get the rest of the text till the end . Special characters are . ( { :
Upvotes: 2
Views: 2639
Reputation: 91385
How about:
^(?:[a-zA-Z ]+[0-9]+ )?([a-zA-Z ,]+)
The text you want is in group 1.
explanation:
^ : begining of string
(?: : start non capture group
[a-zA-Z ]+ : one or more letter or space
[0-9]+ : one or more digit
: a space
)? : end of group optional
( : start capture group 1
[a-zA-Z ,]+ : one or more letter, sapce or coma
) : end of group
Upvotes: 3
Reputation: 5647
I've setup a simple example that will solve your regex by using positive lookahead matching regex:
[\w ]+(?=[.{(;])
The regex above will extract the part before the special characters.
Edit:
For the TEXT0 part is there a specific pattern for it?
Upvotes: 0
Reputation: 11090
str.split("[^\\w\\s]+")[0]
This will match all consecutive [a-zA-Z_0-9] characters and whitespaces from the beginning of the line
List<String> str = new ArrayList<String>();
str.add("TEXT1.more text ");
str.add("TEXT1)more text ");
str.add("TEXT1}more text ");
str.add("Beginning text Text I want . Text I don't want");
str.add("Beginning text with numbers Text I want ( Text I don't want )");
str.add("Beginning text with numbers Text I want { Text I don't want }");
for(String s : str)
System.out.println("input: [" + s + "], first word: " + s.split("[^\\w\\s]+")[0]);
produces:
input: [TEXT1.more text ], first word: TEXT1
input: [TEXT1)more text ], first word: TEXT1
input: [TEXT1}more text ], first word: TEXT1
input: [Beginning text Text I want . Text I don't want], first word: Beginning text Text I want
input: [Beginning text with numbers Text I want ( Text I don't want )], first word: Beginning text with numbers Text I want
input: [Beginning text with numbers Text I want { Text I don't want }], first word: Beginning text with numbers Text I want
Upvotes: 0