-3

Need Regex for

Abc your request is Approved.

In above statement if word 'Approved' comes in sentence it should select whole sentence.

Mr. Abc your request is Approved.

and if word 'Mr' comes in sentence it should not select a sentence even though it contains 'Approved' word.

Pranesh
  • 71
  • 1
  • 1
  • 11
  • 1
    Please try [here](https://regexr.com/). – vinS Dec 13 '17 at 12:03
  • what's keeping you from writing it? – Stultuske Dec 13 '17 at 12:04
  • @vinS I am trying on same [link](https://regexr.com) . I dont know regex much, put regex pattern . – Pranesh Dec 13 '17 at 12:08
  • Please invest some time in learning. Putting a requirement straight away as a question will lead to nothing. As you have seen, your question is already downvoted. BTW, I have not downvoted. Better to try with something and edit your question in order to show your effort. – vinS Dec 13 '17 at 12:14
  • This might help: https://stackoverflow.com/questions/469913/regular-expressions-is-there-an-and-operator/44781456#44781456 – Jiri Tousek Dec 13 '17 at 12:15

3 Answers3

2

((?!Mr).)* matches string that doesn't contain "Mr.". If you check it before and after of the word "Approved", you're done.

^((?!Mr.).)*Approved((?!Mr.).)*$

Example:

Abc your request is Approved. -> Match
Mr. Abc your request is Approved. -> No match
Abc your request is Approved. Mr. -> No match
Abc your Mr. request is Approved. -> No match
Dorukhan Arslan
  • 2,676
  • 2
  • 24
  • 42
1

This should do it

^(?!Mr).*(Approved).*

It will select those lines which doesn't start with Mr, but have the word Approved inside (it can or not be at the end )

Working example here

alseether
  • 1,889
  • 2
  • 24
  • 39
0

Maybe this regex can help you ^(?i)((?=\bApproved\b).)*((?!\bMr\b).)*$

String approved = "Abc your request is Approved.";
String notApproved = "Mr. Abc your request is Approved.";
String regex = "(?i)((?=\\bApproved\\b).)*((?!\\bMr\\b).)*";

If you use :

System.out.println(approved.matches(regex));
System.out.println(notApproved.matches(regex));

Outputs

true
false
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140