Reputation: 61
My strings look like:
some space delimited fixed text: space delimited text: space delimited text...
Example string:
"Mary had a little lamb: it says: baah..."
Here, "Mary had a little lamb: "
string is a constant. It will always be followed by a string which contains alpha numeric characters, colon and space followed by ellipsis. "it says: baah"
may change. ... will always be there.
I have this so far as regex:
"Mary had a little lamb: ^[a-zA-Z:]\\.\\.\\."
but this doesn't work.
I don't know how to incorporate the space after colon in "it says: baah"
. How do I have to write a regular expression that recognizes these strings?
Upvotes: 1
Views: 88
Reputation: 17721
You shouldn't use the beginning operator inside of your regex and your string may include other characters than letters. Try:
"Mary had a little lamb: .*?\\.\\.\\."
The question mark is for non greedy / shortest possible matches. You may remove it to receive longest possible matches.
This more specific pattern requires a colon and restricts the possible characters:
"Mary had a little lamb: [^:]*:[ A-Za-z]*\\.\\.\\."
The [^:]*
matches any string that doesn't contain a colon.
For testing in Java (Bean Shell):
p = java.util.regex.Pattern.compile("Mary had a little lamb: [^:]*:[ A-Za-z]*\\.\\.\\.");
m = p.matcher("Mary had a little lamb: it says: baah...");
print(m.matches());
Upvotes: 2