Solved

Regular expression to locate words ending in a particular format.

Posted on 2013-11-10
18
382 Views
Last Modified: 2013-11-11
Hello Experts,
I need to locate all the words ending with the letters "an" in a text file using notepad++. Can you please suggest some regular expressions for the same?

Thanks.
0
Comment
Question by:sukhoi35
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 4
  • +3
18 Comments
 
LVL 51

Accepted Solution

by:
Rgonzo1971 earned 50 total points
ID: 39637148
Hi,

pls try

\w*an\b

Open in new window


regards
0
 

Author Comment

by:sukhoi35
ID: 39637162
Hey, thanks for the response.
But this didn't work.

Screenshot
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39637166
I used

(an)$
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:sukhoi35
ID: 39637173
Hi marqusG,
Your expression is only locating the last words in every line ending with the letter "an"

For example:

Swan Duck Right Man
Ocean Sea Fish Fan

If I use the expression (an)$ on the above lines, only the words "man" and "fan" are located. Observe that even Swan and Ocean end with "an', but they are not located.
0
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 150 total points
ID: 39637179
You're right:

(an)\b
0
 

Author Comment

by:sukhoi35
ID: 39637184
Even (an)\b isn't working.

Screen capture
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39637188
Don't knowHere it works
0
 
LVL 10

Expert Comment

by:mark_harris231
ID: 39637282
Try .*?an[\s|\r|\n]
0
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 300 total points
ID: 39637461
@mark_harris231, your pattern will fail to match words ending in an which don't have a space character after them. A full stop after the word would cause it to fail.

This worked for me, with the Match case option off:
[a-z]*an(?![a-z])

The (?!...) part of the pattern is called a negative lookahead, which in this case means don't match if a letter is found immediately after "an"

Change the [a-z] to \w if you want to treat the underscore character as a word character. Not that the word "can't" will be found; if that's a problem, I'll adjust the pattern for you.
0
 
LVL 9

Expert Comment

by:Derek Jensen
ID: 39639726
Looking for words ending in 'an':

(\w+?an)[^a-z]

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 39639765
@bigdogdman, that pattern requires a character after the word. For real data, it would probably be fine as a I think the only case it would miss would be a word at the very end of the file, however the negative lookahead works fine in notepad++ and covers all cases.
0
 
LVL 9

Expert Comment

by:Derek Jensen
ID: 39639853
@Terry,

Actually, it doesn't. Granted, I haven't tried it in NotePad++, but unless it has a really sucky Regex engine, [^a-z] should match $ or \n or \r...

Again, untested, so...could still be wrong. :-)
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39639872
Sorry, I'm anovice of regex: can you explain why in my pc works what I shown in post ID 39637188

I don't want points but only understand why (an)\b works in my Notepad++ on Win7 but not for sukhoi35: I really don't understand this...
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 39639875
I tested the negative lookahead in notepad++ and it was fine...

I've never encountered any regex engine that matches an end-of-line or end-of-file (which aren't a character of any sort) with a character set such as [^a-z], but you're correct it will match a \n or \r character, or a literal dollar sign character. Notepad++ works as expected. I'll keep an open mind for other languages though, as some regex engines have some pretty unexpected behaviour!

Note that Notepad++ is a free download, for anyone who hasn't used it before.
0
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 300 total points
ID: 39639924
@marqusG, doing some more testing in Notepad++ (I have version 6.4.2), your original pattern worked fine for me:
\w*an\b

Open in new window

as did the pattern:
(an)\b

Open in new window

though the round brackets are unnecessary.

The only possible faults with it are unlikely to be a problem in real data:
\w will also match an underscore character or number
\b matches a boundary between a word character (\w) and a non-word character. Again, because \w matches an underscore or a number, there may be odd cases missed, although it may not be a problem
eg
ocean1 would not be picked up
ocean_ would not be picked up
With pattern \w*an\b a value of 2345_2345an can be matched.

@sukhoi35, perhaps you'd like to retry using pattern \w*an\b ? It's simpler than the negative lookahead that I suggested.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39639974
Thanks for your answer, Terry: unfortunately, I can't give you points ;)
0
 

Author Closing Comment

by:sukhoi35
ID: 39640660
Thanks!
0
 

Author Comment

by:sukhoi35
ID: 39640664
Hi Guys,
By the way, upgrading to notepad++ v6.5.1 from the existing v5.9.8 helped me use some of the solutions posted earlier. Looks like there were problems with the earlier versions which got fixed recently. Thanks all for your time!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Popup write two lines 3 32
javascript works in Chrome, but none of the other browsers 14 54
Add Rows on a Table 8 54
Replace statements HTML with HTML IF 8 67
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question