tags:

views:

84

answers:

3

Hello guys!

I need a regular expression for findin a pattern. This is the pattern:
id|name|code|mobile
I created a pattern for this if I want to search by id (if id = 1):

.*1.*|.*|.*|.*

But it matches every pattern that contains number 1. What's the problem with it?

+3  A: 

You need to escape the | symbol in regular expressions otherwise it means alternation. It's also a good idea to use anchors if you are in doubt as to whether or not they are required for your regular expression library. This expression matches anything that contains 1 in the id:

^.*1.*\|.*\|.*\|.*$

To match id = 1 exactly, change it to this:

^1\|.*\|.*\|.*$

To match name = 'Foo' exactly:

^.*\|Foo\|.*\|.*$

A good point raised in the comments is that it would be good to use [^|]* instead of .* to ensure that the data has the correct number of pipe symbols.

Note that regular expressions will be a slow way to find your data if you need to make many lookups. It would be faster to first parse the data and then to keep them stored in data structures that allow you to make fast lookups, such as a hash table.

Mark Byers
Another good idea would be to use something like `[^\|]*` in place of `.*`
KT
without the $ sign it works, what does this sign ($) means?
Infinity
@Infinity: End of string or end of line.
KennyTM
Thanks a lot man!
Infinity
+1  A: 

think simple. There is no need for regular expressions. If i get you correct, you are searching for id that is , say 1, and since your data has a distinct delimiter, ie (pipe |), just split your data up into individual parts using pipe as delimiter with your favourite language and check first element (id) against 1 . eg

awk -F"|" '$1==1{print}' file

Python

>>> s="1|John|code|mobile"
>>> if s.split("|")[0] == "1":
...  print "found"
...
found

there should be some string splitting functions that you can use with your preferred language.

ghostdog74
Thanks it is, good but, I need regular expressions because I'm trying to parse a file in Java, I know it is weird, but I need to know the place in the file where the searched string begins. :)
Infinity
+1  A: 

From one of your comments:

Thanks it is, good but, I need regular expressions because I'm trying to parse a file in Java, I know it is weird, but I need to know the place in the file where the searched string begins. :)

Why not hit http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#split(java.lang.String) and use the technique ghostdog74 suggested. A whole-line regex is seriously overkill for what you want to do.

JUST MY correct OPINION
I have a part of code which I can't rewrite... But, it's a good idea.
Infinity