Java regular expression. Alphabetic, with spaces and apostrophe (') - java

what is the pattern to validate the following regular expression in java.
string with only letters, spaces and apostrophes (')
I know that for letters is this ("^[a-zA-Z]+$")
For spaces is this ("\\s)
I don't know what apostrophe is.
But most of all i just want a single expression. Not 3 individual ones.

You can create your own class with the characters that you need to match, like this:
Pattern.compile("[a-zA-Z\\s']+");

there is no single class that could match something so specific, but you can build your own from the existing ones
(\p{Alpha}|\s|')*
matches any number of characters, spaces or apostrophes, in any order.
Take a look at http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html

Pattern p = Pattern.compile("^[a-zA-Z ]*$");
Matcher m = p.matcher("Tester String");
System.out.println(m.matches());// true
Matcher m2 = p.matcher("Tester String 123");
System.out.println(m2.matches());// false
This will accept only alphabets.

Related

Regex to find specific pattern string from string

How to get ricCode:.ABC from following string.
My matcher is
Matcher matcher = Pattern.compile("ricCode:([A-Za-z]+),$").matcher(str);
String str = "{AMX:{ricCode:.ABC,indexDetailEnable:true,price:20,648.15,netChange:<spanclass="md-down">-41.09</span>,percentChange:<spanclass="md-down">-0.20%</span>,tradeDate:17/04/05,tradeTime:16:40,chartDate:17/04/05,chartTime:16:40}";
What is missing in the regex?
Change your regex from this:
ricCode:([A-Za-z]+),$
to this:
ricCode:([A-Za-z.]+)(?=,)
Your original regex would only allow alphabetic characters after ricCode:, but your example has a period . character. Also you were matching the , character, but this would also include the comma in your match, you dont want this - so I added a positive lookahead for the comma so it looks for it there but does not match it. Finally you had the $ character at the end of your regex which matches the end of the string, you dont want to look for the end of the string immediately after the comma, so I removed it.
It helps to use regexr.com to test out your expression.

How to prohibit a backslash with regex in java?

I'm new with regex and just can't find what the regex is to prohibit a backslash.
Thanks for any advice.
EDIT:
I'm using the regex for JTextField, to avoid that the user writes an unvalid input. This regex currently doesnt allow the user to write a space character.
I'm doing this with
String regex = "\\S{1}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
So how I change my regex to prohibit backslash as well?
Based on your example snippet, the following expression should work similarly, but also disallowing the backslash:
String regex = "[^\\s\\\\]{1}";
It is a bit strange that you are looking for a one-char non-space and non-backslash pattern, but I guess you are iterating through and checking for consecutive matches.
I would use the following regex though:
String regex = "[^\\s\\\\]+";
and check whether it matched the whole String (matcher.matches()).
The regex pattern with java is \\\\
String somestring;
somestring = somestring.replaceAll("\\\\", "");
Would remove them. Semantically it equates down to \\ at the regex level, which becomes a literal \ match.
You can also use a Pattern match if you want to just compare, or just use String#contains
String somestring;
if (somestring.contains("\\")) {...}
To test if a string has a backslash:
if (input.matches(".*\\\\.*"))
To remove backslashes from strings:
input = input.replace("\\", "");
Note the use of the non-regex based replace() method.

Pattern Matching for java using regex

I have a Long string that I have to parse for different keywords. For example, I have the String:
"==References== This is a reference ==Further reading== *{{cite book|editor1-last=Lukes|editor1-first=Steven|editor2-last=Carrithers|}} * ==External links=="
And my keywords are
'==References==' '==External links==' '==Further reading=='
I have tried a lot of combination of regex but i am not able to recover all the strings.
the code i have tried:
Pattern pattern = Pattern.compile("\\=+[A-Za-z]\\=+");
Matcher matcher = pattern.matcher(textBuffer.toString());
while (matcher.find()) {
System.out.println(matcher.group(0));
}
You don't need to escape the = sign. And you should also include a whitespace inside your character class.
Apart from that, you also need a quantifier on your character class to match multiple occurrences. Try with this regex:
Pattern pattern = Pattern.compile("=+[A-Za-z ]+=+");
You can also increase the flexibility to accept any characters in between two =='s, by using .+? (You need reluctant quantifier with . to stop it from matching everything till the last ==) or [^=]+:
Pattern pattern = Pattern.compile("=+[^=]+=+");
If the number of ='s are same on both sides, then you need to modify your regex to use capture group, and backreference:
"(=+)[^=]+\\1"

How can i add multiple match conditions in a regex

I have a String like this : String x = "return function ('ABC','DEF')";
I am using this:
Pattern pattern = Pattern.compile("'(.*?)'");
Matcher matcher = pattern.matcher(formula);
while (matcher.find()) {
System.out.println("------> " + matcher.group();
}
to retrieve strings between single quotes.
My question is: how can i adapt this regex so that it will check for strings between single quotes AND strings like " ,'DEF' " (meaning which start with ,' and end with ')?
You can use this pattern:
'[^']+'|"[^"]+"
Just to match with empty quoted string change '+' to '*'.
See test.
This pattern should do what you want:
"(?:,\s*)?'[^']*'"
The ? means the first group will match zero or one times.
I used (?:...) because this is a non-capturing group. It is better to use when you don't need to capture that portion of the match.
Also, I replaced .*? with [^']*, meaning the single-quoted string contains anything that is not a single quote. This is more efficient and less likely to lead to mistakes in your regex than .*?.
(Note: this regex allows there to be space between the comma and the start of the string. At first looking at your example, I thought that was true of your example. But now I see that it is not. Still, that might be useful depending on what your data looks like).
You could use the regex pattern:
Pattern.compile(",?'(.*?)'");
,? means 0 or 1 commas. The ? is greedy, so if there is a comma, it will be included in the match.
So: This will match:
A comma, followed by a string enclosed in single quotes
OR.. only a string enclosed in single quotes

Java Regex for username

I'm looking for a regex in Java, java.util.regex, to accept only letters ’, -, and . and a range of Unicode characters such as umlauts, eszett, diacritic and other valid letters from European languages.
What I don't want is numbers, spaces like “ ” or “ Tom”, or special characters like !”£$% etc.
So far I'm finding it very confusing.
I started with this
[A-Za-z.\\s\\-\\.\\W]+$
And ended up with this:
[A-Za-z.\\s\\-\\.\\D[^!\"£$%\\^&*:;##~,/?]]+$
Using the cavat to say none of the inner square brackets, according to the documentation
Anyone have any suggestions for a new regex or reasons why the above isn't working?
For my answer, I want to use a simpler regex similar to yours: [A-Z[^!]]+, which means "At least once: (a character from A to Z) or (a character that is not '!').
Note that "not '!'" already includes A to Z. So everything in the outer character group([A-Z...) is pointless.
Try [\p{Alpha}'-.]+ and compile the regex with the Pattern.UNICODE_CHARACTER_CLASS flag.
Use: (?=.*[##$%&\s]) - Return true when atleast one special character (from set) and also if username contain space.
you can add more special character as per your requirment. For Example:
String str = "k$shor";
String regex = "(?=.*[##$%&\\s])";
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(str);
System.out.println(matcher.find()); => gives true

Categories

Resources