开发者

Regular expressions

I need a regular exp开发者_如何学Goression 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?


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.


I thought your requirement was that the ID must begin with "1", so I created a sample code in Perl, with a simple regex. Feel free to ask more questions.

my $pattern1="1|ee|ew|56";
my $pattern2="001|90|34|06";
my $pattern3="009|56|09|16";
my $pattern4="003|67|87|76";

if ($pattern1 =~ m/^1/){
print "Match Found for : $pattern1\n";
}
elsif ( $pattern2 =~ m/^1/){
print "Match Found for : $pattern2\n";
}
elsif ($pattern3 =~ m/^1/){
  print "Match Found for : $pattern3\n";
}
elsif ($pattern4 =~ m/^1/){
  print "Match Found for : $pattern4\n";
}
else{
  print "No Match Found";
}


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜