Re: Fine tuning a reg exp (\w)
by tye (Sage) on Feb 23, 2012 at 15:27 UTC
|
\w includes [a-zA-Z] (and others) so [A-Z \w] is exactly the same as [ \w] and certainly matches a-z.
| [reply] [d/l] [select] |
|
|
Thanks, still learning reg exps. Even if I do this:
^([A-Z]+\s[A-Z]+,)
I still get lowercase characters:
ABU BAKR
Sabaqah School
| [reply] [d/l] [select] |
|
|
I don't get that output with your regex:
use warnings;
use strict;
while (<DATA>) {
print if /^([A-Z]+\s[A-Z]+,)/;
}
__DATA__
ABU BAKR, Ibrahim Ali Muhammad (a.k.a. AL-LIBI, Abd al-Muhsin)
(individual) [SDGT]
AFGHAN SUPPORT COMMITTEE (ASC) (a.k.a. AHYA UL TURAS; a.k.a. JAMIAT
AYAT-UR-RHAS AL ISLAMIA; a.k.a. JAMIAT IHYA UL TURATH AL ISLAMIA;
a.k.a. LAJNAT UL MASA EIDATUL AFGHANIA) Grand Trunk Road, near
Pushtoon Garhi Pabbi, Peshawar, Pakistan; Cheprahar Hadda, Mia Omar
Sabaqah School, Jalalabad, Afghanistan [SDGT]
prints:
ABU BAKR, Ibrahim Ali Muhammad (a.k.a. AL-LIBI, Abd al-Muhsin)
See http://sscce.org | [reply] [d/l] [select] |
|
|
| [reply] |
|
|
perl -e 'print "Sabaqah School," =~ /^([A-Z]+\s[A-Z]+),/ ? "Yes" : "No
+"'
No
| [reply] [d/l] |
|
|
Try posting your complete regex from "s" through ";" (e.g. s/([ A-Z]+)/myreplacement/gixm; -- it will make it clear what you're actually applying.
Even better, post a small chunk of runnable code. It sounds from previous posts that you're just reading in lines and processing them in a pretty straightforward way, so the code should be quite short.
A couple hours with a regex tutorial would also help you get going faster- the questions you're posting are chapter 1 or 2 kind of things, because they're almost the first thing everyone wants to do.
| [reply] [d/l] |
|
|
while (<DATA>) {
/([A-Z]+)/i; # upper AND lower!
print $1,"\n";
}
output :
ABU
individual
AFGHAN
AYAT
a
Pushtoon
Sabaqah
| [reply] [d/l] |
Re: Fine tuning a reg exp
by runrig (Abbot) on Feb 23, 2012 at 15:52 UTC
|
See perlre. All uppercase can be matched with: /^[[:upper:]]+$/
Update: This is unnecessary and doesn't help much. [A-Z] does just as well unless you consider unicode... | [reply] [d/l] [select] |
|
|
Thanks all, this is all really useful. I think I've got this bit now, but using the same text as an example:
ABU BAKR, Ibrahim Ali Muhammad (a.k.a. AL-LIBI, Abd al-Muhsin)
(individual) [SDGT]
AFGHAN SUPPORT COMMITTEE (ASC) (a.k.a. AHYA UL TURAS; a.k.a. JAMIAT
AYAT-UR-RHAS AL ISLAMIA; a.k.a. JAMIAT IHYA UL TURATH AL ISLAMIA;
a.k.a. LAJNAT UL MASA EIDATUL AFGHANIA) Grand Trunk Road, near
Pushtoon Garhi Pabbi, Peshawar, Pakistan; Cheprahar Hadda, Mia Omar
Sabaqah School, Jalalabad, Afghanistan [SDGT]
I'm now trying to match firstnames. What reg exp is need to match
Ibrahim Ali Muhammad
the reason being is that I'm trying to add tags to a text document, so that I can work manipulate it like this:
$line =~ s/regexp\<name\>$1\<\/name\>/;
I want to achieve this:
ABU BAKR, <name>Ibrahim Ali Muhammad</name> (a.k.a. AL-LIBI, Abd al-Mu
+hsin)
(individual) [SDGT]
AFGHAN SUPPORT COMMITTEE (ASC) (a.k.a. AHYA UL TURAS; a.k.a. JAMIAT
AYAT-UR-RHAS AL ISLAMIA; a.k.a. JAMIAT IHYA UL TURATH AL ISLAMIA;
a.k.a. LAJNAT UL MASA EIDATUL AFGHANIA) Grand Trunk Road, near
Pushtoon Garhi Pabbi, Peshawar, Pakistan; Cheprahar Hadda, Mia Omar
Sabaqah School, Jalalabad, Afghanistan [SDGT]
| [reply] [d/l] [select] |
|
|
Is there anything (punctuation, perhaps? placement with other words and terms?) that will consistently distinguish a name from any other proper noun in your text? For example, how can your script consistently distinguish between "Ibrahim Ali Muhammad" and "Grand Trunk Road" and "Pushtoon Garhi Pabbi", since all use the same capitalization scheme? You might have to define some more complicated criteria for recognizing names. Or will names only be in the headings of each entry, i.e. toward the beginning?
In general, you would want:
$line =~ s{($regexp)}{<name>$1</name>}g;
The 'g' flag may or may not be needed, depending on what you're doing. If there's more than one name in a line, that would catch it. If there's only one name, you don't need it. The parentheses () match the name in your line and place it in $1, so you can put the tags around it in your replacement expression. Using curly brackets {} instead of / to mark your regexp avoids having to escape your slashes ("leaning toothpick syndrome," I think someone called it -- it can get confusing!). Any other characters could be used to delimit your regexp if you'd prefer. What I have above is equivalent to this:
$line =~ s/($regexp)/<name>$1<\/name>/g;
| [reply] [d/l] [select] |
|
|
|
|
|
|
| [reply] [d/l] |
|
|
Because the former works in a shell and sed, too?
| [reply] |
|
|
Re: Fine tuning a reg exp
by Marshall (Canon) on Feb 23, 2012 at 21:08 UTC
|
Before proceeding further with this, I think that it should be noted that "SDGT" is a buzzword for "Specially Designated Global Terrorists".
Al-Libi, Abd al-Muhsin or Ibrahim Ali Muhammad is on the "Most Wanted Terrorist" list.
Normally I would help anybody with anything related to Perl.
However in this case, I would like to hear more about who you are and why you are doing this? And why you do not have access to the more easily parseable databases?
I hope that you realize that parsing a terrorist list is a very touchy subject.
Update: If you are getting this info from a public URL, then post that URL. Posting anything like this from a US government internal database, even just a short excerpt, is not appropriate here. | [reply] |
|
|
| [reply] |
|
|
I think the OP should post the URL that he is working from.
Working from the whole list will make it easier to understand and parse out what he needs. If the info is public, I have no problem with making it "easier to understand" via re-formatting. And I would help with that.
I personally feel "very uncomfortable" if the full info is not available to the general public and the OP's info looks more specific than what I could find. Something like this has not come up before in my time on Monks.
Your URL comes up with:
ABU BAKR, Ibrahim Ali Muhammad (a.k.a. AL-LIBI, Abd al-Muhsin;
a.k.a. SABRI, Abdel Ilah; a.k.a. TANTOUCHE, Ibrahim Abubaker;
a.k.a. TANTOUSH, Ibrahim Abubaker; a.k.a. TANTOUSH, Ibrahim Ali Abu
Bakr; a.k.a. "'ABD AL-MUHSI"; a.k.a. "'ABD AL-RAHMAN"; a.k.a. "ABU
ANAS"); DOB 1966; alt. DOB 27 Oct 1969; nationality Libya; Passport
203037 (Libya) (individual) [SDGT]
Fine. Yes. I know this guy in on the terrorist list.
But it doesn't show all of the info that the OP had although it shows additional information.
I think my pointing out that this is a terrorist list was appropriate. Let's see what the OP has to say and we go from there. | [reply] [d/l] |
|
|
|
|
|
|
|
|
|
|