It appears to work because the single period is an "any character" wildcard. Thus, it finds the period in your "U.S.", but in addition it also matches any other character, including a preceding space, as you have seen.
Use \. to escape the regular meaning of the period and make it behave as the litera code.
In additionI made a litte error in the code above. It will only find a word with one period in it. So rewritten it should be:
However, I found that following also works:
Tom Tomasko wrote:
[...] However, I found that following also works:
Yes it will work, and as a side effect ... it will also pick up '+' and '*' !
A list of characters inside [ Character Set brackets ] loose the magical properties they have outside them. \w is still regarded as 'any word character' (0..9, A..z, and the equivalent in other scripts), but the period does not longer match "any character" but only itself. There is no need to grab more characters using either + or * because anything inside the Character Set brackets always will match one single character, and you need the '+' right after it to get it to repeat.
So all you need is this:
( Begin Group #1
[ Inclusion: any character in this group
\w Any word character (A..Z, a..z, _, 0..9)
. The character “.”
] End Inclusion Group
+ Any character in this group may occur once or more times; longest possible match will be taken
) End Group #1
- Literal character “-”
( Begin Group #2
\w+ Any word character (A..Z, a..z, _, 0..9); may occur once or more times; longest possible match will be taken
) End Group #2
Thanks Jongware for that correction and fuller explanation.
hi use this below i mentioned grep. your will the correct answer
No it will certainly not result in "the correct answer".
Apart from not fulfilling the primary object, your expression has lots of unexpected -- and probably unwanted -- side-effects.