find if a string ends with semicolon or a word - java

I need a regex to determine if a string ends with semicolon or "BEGIN" or "THEN". Also before BEGIN and THEN words, there must be a white space or line break character.
if(strLineText.matches(";|THEN|BEGIN$"))
This works for THEN and BEGIN but not for semicolon. And also with this regex I could not determine if THEN and BEGIN are exact words.

You need to put them inside a group.
if(strLineText.matches("(?s).*(?:;|\\bTHEN|\\bBEGIN)$"))
or
if(strLineText.matches("(?s).*(?:;|\\sTHEN|\\sBEGIN)$"))

You can use a simple lookahead for the same.
^(?=.*(?:;|[ \\n]THEN|[ \\n]BEGIN)$).*$

This is not a regex.
You can use .endsWith() method too.
String str = "hey;";
if(str.endsWith(";"))
System.out.println("Ends with a ;");
public static boolean endsWithWord(String str, String word)
{
return str.endsWith(word);
}
System.out.println(endsWithWord("hey;", ";"));
System.out.println(endsWithWord("umm BEGIN", "BEGIN"));
System.out.println(endsWithWord("umm THEN", "THEN"));

Related

regular expressions to determine if a string starts with ;

The requirement is simple: if the given string matches:
starts with ';'
starts with some char or chars among '\r','\n','\t',' ', and then followed with ';'.
For example ";", "\r;","\r\n;", " \r\n \t;" should all be ok.
Here is my code and it does not work:
private static String regex = "[\\r|\\n| |\\t]+;";
private static boolean startsWithSemicolon(String str) {
return str.matches(regex);
}
Thanks for any help.
You have 2 choices:
Use matches(), in which case the regex must match the entire input, so you'd have to add matching of characters following the ;.
Regex: str.matches("[\\r\\n\\t ]*;.*")
or: Pattern.compile("[\\r\\n\\t ]*;.*").matcher(str).matches()
Use find(), in which case the regex must be anchored to the beginning of the input:
Regex: Pattern.compile("^[\\r\\n\\t ]*;").matcher(str).find()

Removing string before open parenthesis using regular expression

I have an expression Nvl(cost,Sum(cost1))
i need to remove string before paranthesis i.e NVl and Sum in this case
String functions=externalFormat.replaceAll("\\([^\\(]*\\)", "");
Input Nvl(cost,Sum(Cost1)
Output cost,cost1
If you can't use capture groups (ie, if you CAN use capture groups, (\w*?)\( will capture the text you need to replace in the first group)
You could use a positive look-ahead to only capture word characters (letter or number) that appear before an open bracket:
\w*(?=\()
You could even add optional white space characters in case of things like: Nvl1 (cost,Sum (cost1)) by including them before the look-ahead: -
\w*\s*(?=\()
Hope this helps solve your problem.
Check below code :
public static void main(String[] args) {
String externalFormat="Nvl(cost,Sum(Cost1)";
String functions=externalFormat.replaceAll("\\w+\\(|\\)", "");
System.out.println(functions.toLowerCase());
}

Regex: replace whitespaces with hyphens and allow only a-z

I'm struggling with regex here.
How do I replace whitespaces with hyphens and allow only a-z symbols?
public String filterSpeciesName(String species) {
return species.replaceAll("[^a-zA-Z]", "").toLowerCase();
}
An example would be
input string "Bar''r$ack Put1in"
output string "barrack-putin"
return species.trim().replaceAll("\\s", "-").replaceAll("[^a-zA-Z-]", "").toLowerCase();
To replace any space character by hyphens, use String#replaceAll("\\s", "-").
Then, if you want to simply remove the characters that are not a-z, use replaceAll("[^a-zA-Z-]", ""), assuming you don't want to get rid of your newly added hyphens :)
But I would rather recommend you to maybe just:
match if species.replaceAll("\\s", "-") matches ^[a-zA-Z-]+$
throw an Exception if this is not the case
return the formatted value otherwise

Java using regex to verify an input string

g.:
String string="Marc Louie, Garduque Bautista";
I want to check if a string contains only words, a comma and spaces. i have tried to use regex and the closest I got is this :
String pattern = "[a-zA-Z]+(\\s[a-zA-Z]+)+";
but it doesnt check if there is a comma in there or not. Any suggestion ?
You need to use the pattern
^[A-Za-z, ]++$
For example
public static void main(String[] args) throws IOException {
final String input = "Marc Louie, Garduque Bautista";
final Pattern pattern = Pattern.compile("^[A-Za-z, ]++$");
if (!pattern.matcher(input).matches()) {
throw new IllegalArgumentException("Invalid String");
}
}
EDIT
As per Michael's astute comment the OP might mean a single comma, in which case
^[A-Za-z ]++,[A-Za-z ]++$
Ought to work.
Why not just simply:
"[a-zA-Z\\s,]+"
Use this will best
"(?i)[a-z,\\s]+"
If you mean "some words, any spaces and one single comma, wherever it occurs to be" then my feeling is to suggest this approach:
"^[^,]* *, *[^,]*$"
This means "Start with zero or more characters which are NOT (^) a comma, then you could find zero or more spaces, then a comma, then again zero or more spaces, then finally again zero or more characters which are NOT (^) a comma".
To validate String in java where No special char at beginning and end but may have some special char in between.
String strRGEX = "^[a-zA-Z0-9]+([a-zA-Z0-9-/?:.,\'+_\\s])+([a-zA-Z0-9])$";
String toBeTested= "TesADAD2-3t?S+s/fs:fds'f.324,ffs";
boolean testResult= Pattern.matches(strRGEX, toBeTested);
System.out.println("Test="+testResult);

Remove end of line characters from end of Java String

I have a string which I'd like to remove the end of line characters from the very end of the string only using Java
"foo\r\nbar\r\nhello\r\nworld\r\n"
which I'd like to become
"foo\r\nbar\r\nhello\r\nworld"
(This question is similar to, but not the same as question 593671)
You can use s = s.replaceAll("[\r\n]+$", "");. This trims the \r and \n characters at the end of the string
The regex is explained as follows:
[\r\n] is a character class containing \r and \n
+ is one-or-more repetition of
$ is the end-of-string anchor
References
regular-expressions.info/Anchors, Character Class, Repetition
Related topics
You can also use String.trim() to trim any whitespace characters from the beginning and end of the string:
s = s.trim();
If you need to check if a String contains nothing but whitespace characters, you can check if it isEmpty() after trim():
if (s.trim().isEmpty()) {
//...
}
Alternatively you can also see if it matches("\\s*"), i.e. zero-or-more of whitespace characters. Note that in Java, the regex matches tries to match the whole string. In flavors that can match a substring, you need to anchor the pattern, so it's ^\s*$.
Related questions
regex, check if a line is blank or not
how to replace 2 or more spaces with single space in string and delete leading spaces only
Wouldn't String.trim do the trick here?
i.e you'd call the method .trim() on your string and it should return a copy of that string minus any leading or trailing whitespace.
The Apache Commons Lang StringUtils.stripEnd(String str, String stripChars) will do the trick; e.g.
String trimmed = StringUtils.stripEnd(someString, "\n\r");
If you want to remove all whitespace at the end of the String:
String trimmed = StringUtils.stripEnd(someString, null);
Well, everyone gave some way to do it with regex, so I'll give a fastest way possible instead:
public String replace(String val) {
for (int i=val.length()-1;i>=0;i--) {
char c = val.charAt(i);
if (c != '\n' && c != '\r') {
return val.substring(0, i+1);
}
}
return "";
}
Benchmark says it operates ~45 times faster than regexp solutions.
If you have Google's guava-librariesin your project (if not, you arguably should!) you'd do this with a CharMatcher:
String result = CharMatcher.any("\r\n").trimTrailingFrom(input);
String text = "foo\r\nbar\r\nhello\r\nworld\r\n";
String result = text.replaceAll("[\r\n]+$", "");
"foo\r\nbar\r\nhello\r\nworld\r\n".replaceAll("\\s+$", "")
or
"foo\r\nbar\r\nhello\r\nworld\r\n".replaceAll("[\r\n]+$", "")

Categories

Resources