String match of only 3 specific words
You can use include all characters you are interested in within a square bracket. To match one or more occurrences of these characters in square brackets, append +
to it. The message
string should be entirely made up of only these characters for it to be considered a match.
Try this.
String message = "ADAIAIAIAIAIADDDAI";
if(message .matches("[ADI]+")) {
System.out.println("Matches");
}
match 3 words in a paragraph only using one regex
How about just
/\b(Spotify-Apple|US10|extra functions)\b/g
Demo: https://regex101.com/r/1Z96tS/3
Regex for finding words containing more then 3 'a' characters
I would keep it simple and just use:
\b\w*[Aa]\w*[Aa]\w*[Aa]\w*\b
Demo
This regex pattern matches any word containing three lower/upper a/A characters in it, appearing anywhere in the word.
Regular Expression for recognizing the string that contains only specific words
^(?!\s*$)(?:Seven|Six|Two|One|Three| )+$
Try this.See demo.
https://regex101.com/r/tJ2mW5/6
Match words only if preceded by specific pattern
Assuming the fields you want to extract are always in capital letters and preceded by 6 digits and a space, this regular expression would do the trick:
(?<=\d{6}\s)[A-Z]+
Demo: https://regex101.com/r/dsDHTs/1
Edit: if you want to match up to two alpha-numeric uppercase words preceded by 6 digits, you can use:
(?<=\d{6}\s)([A-Z0-9]+\b)\s(?:([A-Z0-9]+\b))*
Demo: https://regex101.com/r/dsDHTs/5
If you have a specific list of valid fields, you could also simply use:
(AAD|TMLB|RECHNX|RR4HNX)
https://regex101.com/r/dsDHTs/3
match string if does not contain a specific word
You need the following regex
/^(?!.*TOTO)(.*)$/s
Try it at Regex 101 to get an explanation as to what it does
Regex for matching specific words or their combination as well as the exception word
You might use
\b(?>DELETE(?: FROM)?|FROM) (?!DUAL\b)\w+\b(?!\.)
The pattern matches:
\b
Word boundary to prevent a partial word match(?>DELETE(?: FROM)?|FROM)
Atomic group (No backtracking) to match either DELETE with optional FROM or only FROM(?!DUAL\b)
Negative lookahead to assert not the word DUAL directly to the right followed by a word boundary\w+\b
Math 1+ word characters and a word boundary(?!\.)
Negative lookahead, assert not a dot directly to the right of the current position
.NET Regex demo
Regex to match specific word
Replace your pattern with:
string pattern = @"(Key:\bApple\b)|(Key: \bApple\b)";
The \b
checks the word boundaries and is able to evaluate count as 1 with your example.
You can read more about it here.
Rego regex to match a specific word from a sentence
You can use
regex.match("\\blatest?\\b", m)
or
regex.match(".*\\blatest?\\b.*", m)
See the Rego playground demo.
Note that (t|)?
is a capturing group that matches a t
or empty string, and is basically equal to t?
pattern (there is no need to capture the t
) that matches an optional t
char.
Related Topics
Java Fast Food Menu (Using Methods)
Spring @Value Annotation Always Evaluating as Null
Remove Trailing Comma from Comma-Separated String
Regular Expression for Excluding Special Characters
Transactionrequiredexception Executing an Update/Delete Query
Calling Method That Exists in Child Classes But Not in Parent Class
How to Replace a Placeholder in a String With a Simpledateformat Pattern
Jsp and CSS File Not Loading in Browser Java Webapp
Method in Class Cannot Be Applied to Given Types. Required: No Arguments
How to Modify Log4J Jsonlayout Field Names
How to Check If an Excel Cell Is Empty Using Apache Poi
Enter Array Without Knowing Its Size
How to Have Each Record of Json on a Separate Line
Java:Using Apache Poi How to Convert Ms Word File to Pdf
Mockito How to Mock Only the Call of a Method of the Superclass