1

There are now different requirements to the regex I am looking for, and it is too complex to solve it on my own.

I need to search for a specific string with the following requirements:

  1. String starts with "fu: and ends with "

  2. In between those start and end requirements there can be any other string which has the following requirements:

    2.1. Less than 50 characters

    2.2. Only lower case

    2.3. No trailing spaces

    2.4. No space between "fu: and the other string.

The result of the regex should be cases where case no' 1 matches but cases no' 2./2.1/2.2/2.3/2.4 don't.

At the moment I have following regex: "fu:([^"]*?[A-Z][^"]*?)", which finds strings with start with "fu: and end with " with any upper case inbetween like this one:

"fu:this String is wrong cause the s from string is upper case"

I hope it all makes sense, I tried to get into regex but this problem seems to complex for someone who is not working with regex every day.

[Edit]

Apparently I was not clear enough. I want to have matches which are "wrong". I am looking for the complement of this regex: "fu:(?:[a-z][a-z ]{0,47}[a-z]|[a-z]{0,2})"

some examples:

Match: "fu: this is a match"

Match: "fu:This is a match"

Match: "fu:this is a match "

NO Match: "fu:this is no match"

Sorry, its not easy to explain :)

4

4 回答 4

2

Try the following:

"fu:([a-z](?:[a-z ]{0,48}[a-z])?)"

This will match any string that begins with "fu: and ends with a " and the string between those will contain 1-50 characters - only lower-case and not able to begin with a space nor have trailing spaces.

"fu:                    # begins with "fu:
(                       # group to match
    [a-z]               # starts with at least one character
    (?:                 # non-matching sub-group
        [a-z ]{0,48}    # matches 0-48 a-z or space characters
        [a-z]           # sub-group must end with a character
    )?                  # group is not required
)
"                       # ends with "

EDIT: In the event that you need an empty-string to match too, i.e. the full string is "fu:", you can add another ? to the end of the matching-group in the regex:

"fu:([a-z](?:[a-z ]{0,48}[a-z])?)?"

I've kept the two regexes separated (one that allows 1-50 characters in the string and one that allows 0-50) to show the minor difference.

EDIT #2: To match the inverse of the above, i.e. - to find all strings that do not match the required format, you can use:

^((?!"fu:([a-z](?:[a-z ]{0,48}[a-z])?)?").)*$

This will explicitly match any line that does not match that pattern. This will consequently also match lines that do not contain "fu: - if that matters.

The only way I can figure out to truly match the opposite of the above and still include the anchors of "fu: and " are to explicitly attempt to match the rules that fail:

"fu:([^a-z].*|[^"]{51,}|[a-z]([^"]*?[A-Z][^"]*?)+|[a-z ]{0,49}[ ])"

This regex will match anything that starts with not a lowercase a-z character, any string that's longer than 50 characters, any string that contains an uppercase letter, or any string that has trailing whitespace. For each additional rule, you'll need to update the regex to match the opposite of what's needed.

My recommendation is, in whatever language you're using, to match all input strings that actually follow your requirements - and if there are no matches then that string must violate your rules.

于 2012-08-14T14:02:17.117 回答
0
"fu:([^A-Z" ](?:[^A-Z"]{0,48}[^A-Z" ])?)"

The above regex should match the specified requirements.

于 2012-08-14T14:02:09.947 回答
0

这可能就是你需要的

"fu:([a-z](?:[a-z ]{,48}[a-z])?)"
于 2012-08-14T14:12:33.923 回答
0

尝试这个:

"fu:(?:[a-z][a-z ]{0,47}[a-z]|[a-z]?)"
于 2012-08-14T14:01:29.753 回答