1

I need Regex to filter string to fit with the following conditions :

  • The total length of the string cannot exceed 300 characters.
  • The string cannot contain more than 4 hashtags.
  • The string cannot contain more than 1 URL.
  • The string cannot consist of all capital letters.

Example Valid Text :

Developers trust #StackOverflow to help solve #coding problems and use Stack Overflow Careers to find job opportunities. Please visit https://stackoverflow.com/

Example Invalid Text :

Developers trust #StackOverflow to help solve #coding problems and use Stack Overflow Careers to find job opportunities. Please visit https://stackoverflow.com/ or https://stackoverflow.com/company/about

Invalid string above because there are containing two URL.

Any help would be appreciated and thank you very much :)

8
  • Defined what an "URL" is. Commented Aug 16, 2016 at 4:59
  • @anshul-rai : Nope :( because I don't know much about regex Commented Aug 16, 2016 at 5:03
  • @derek-朕會功夫 : any URL... Commented Aug 16, 2016 at 5:03
  • @PeterChimp Well what do you mean by an URL? Commented Aug 16, 2016 at 5:03
  • @derek-朕會功夫 you can see on example Valid / Invalid string above... Commented Aug 16, 2016 at 5:34

1 Answer 1

3

I think this will help you

^(?!^([^#]*#[^#]*){5,}$)(?!(.*http(s{0,1}):\/\/){2,})(?=.*[a-z]).{0,300}$
  • (?!^([^#]*#[^#]*){5,}$) this will ensure that string should not contain more then 4 #
  • (?!(.*http(s{0,1}):\/\/){2,}) this will ensure that string should not contain more then 2 url
  • (?=.*[a-z]) this will ensure that string should contain at least 1 lower case letter
  • .{0,300} this will ensure that string should contain not more then 300 charter
Sign up to request clarification or add additional context in comments.

1 Comment

I would use str.length<=300 instead of regex by checking the length. I think trying to do everything in a single regex is anti-pattern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.