简体   繁体   中英

How to extract a string from text that is 50 chars long and consists of A to Z and 0-9 .. starts with a capital letter

example string

 &+^'<>123Abcdiiiiiiiiiiiiii iiiiiiiiiiiii iiii iiiiiii iiii iii iiiiiiiiiiiiiiiiiiiiiii iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii iiiiiiiiii iiiiiiiiii iiiiiiiii iiiiii.!'^+%&/()

from that string the regex code needs to ask for a string that starts with a capital letter and ends with a dot but is at least 50 characters long and can consist of only 0-9 and AZ in a case insensitive manner and may also contain double quotes or single quotes as well as dots.

python is the language of choice to test the regex code.

Im not exactly sure what you are asking, but I assumed you wanted a regex that would grab a string at least 50 chars long, contains only letters and numbers and quotes and ends in a dot.

([A-Z][A-Za-z0-9'".]{48,}[.])

正则表达式可视化

This one does not validate that there are numbers in the string. In order to do that you also need a look ahead before you extract the string. This is usually much more advanced and gives you more trouble than its worth. Therefore, it would be better to simply EXTRACT the one above, then VALIDATE it against a second regex

(.*[0-9].*)

这应该有效。

/^[A-Z][A-z \.'"]{50,}\.$/
reobj = re.compile(r"""(?=[A-Z])([A-Za-z0-9"'. ]{50,}\.)""")
result = reobj.findall(subject)

http://regex101.com/r/eH3qP2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM