String replacement in Perl after comparing with the original string - regex

I want to compare a string in perl below is detail description
original string
../db/proj/upload/1/22352/eng_wall_paper.jpg
I need to extract the file name eng_wall_paper.jpg from the string
and compare it with variable and append the new variable to the string.
new required string
../db/proj/upload/1/22352/new string.jpg
how can it be done, thanks in advance .

Now as you can see in the comment in your question that you really need to be specific about where you are actually stuck in to ask a question in SO. However, after looking at your other questions in SO it looks like you are completely new in programming world and need serious helping hand. Hence I think it would be helpful for you to get some useful reference to solve your problem.
If I breakdown your main problem statement I can see three parts
1. Need to extract the file name,
Most recommended way to do it is using File::Basename. However, its possible to use regex too. You need to learn how to write regex and how to capture a group. This link should be really helpful.
2. Compare it with variable,
Whichever method you use from above you should get the filename in a variable or in $1. Just compare that whatever you want to compare with. Make sure to use eq instead of ==. Find more details here.
3. Append the new variable to the string,
By now you should have the old filename in a variable, say $oldname, from first step and the new filename in another variable, say $newfilename. This question is already answered several times in SO, like this one. Hope that helps.
The actual solution is merely a 3 or 4 lines of code which I want you to figure out. Good luck.

Related

Trying to eliminate second regex exec

I am wondering if there is a way to declare boundaries other start of line or end of line but based on a value in the text. I am trying to optimize my code and right now I find a section in my doc and extract it based on a regular expression. Then I run that extracted section through another expression.
For simplicity my text looks like the
<start><doc><font>123</font></doc><doc><font>234</font></doc><doc><font>345</font></doc><doc><font>456</font></doc><end>
Since my <start> is not the start but somewhere in doc I have to find that. I assume if its possible it should be more effective then running two expr exec's to get the data. Anything small will help as my script will have to run at least one million times.
Not really sure about the efficiency, if your data would be as simple and clean as it is printed in the question, this expression might be an start:
(<start>(<doc>(<font>.*?<\/font>)<\/doc>)<end>)
Otherwise, you might want to clean your data first, and maybe find some alternative solutions.
DEMO

Parse a string for open and close tags

Let's say I have the following strings:
"This [color=RGB]is[\color] a string."
"This [color=RGB][bold]is[\bold][\color] another string."
What I'm looking for is a good way to parse the string in order to extract the tag information and then reconstruct the original string without tags.
The tag informations will be used during text rendering.
Obviously I can achieve the goal by working directly with strings (find/substr/replace and so on), but I'm asking if there is another way cleaner, for example using regular expression.
Note:
There are very few tags I need, but there is the possibility to nest them (only of different type).
Can't use Boost.
There's a very simple answer that might work, depending on the complexity of your strings. (And me understanding you correctly, i.e. you just want to get the cleaned up strings, not actually extract the tags.) Just remove all tags. Replace
\[.*?]
with nothing. Example here
Now, if your string should be able to contain tag-like objects this might not work.
Regards

How to create a regex to check whether a set of words exists in a given string?

How can I write a regex to check if a set of words exist in a given string?
For example, I would like to check if a domain name contains "yahoo.com" at the end of it.
'answers.yahoo.com', would be valid.
'yahoo.com.answers', would be wrong. 'yahoo.com' must come in the end.
I got a hint from somewhere that it might be something like this.
"/^[^yahoo.com]$/"
But I am totally new to regex. So please help with this one, then I can learn further.
When asking regex questions, always specify the language or application, too!
From your history it looks like JavaScript / jQuery is most likely.
Anyway, to test that a string ends in "yahoo.com" use /.*yahoo\.com$/i
In JS code:
if (/.*yahoo\.com$/i.test (YOUR_STR) ) {
//-- It's good.
}
To test whether a set of words has at least one match, use:
/word_one|word_two|word_three/
To limit matches to just the most-common, legal sub-domains, ending with "yahoo.com", use:
/^(\w+\.)+yahoo\.com$/
(As a crude, first pass)
For other permutations, please clarify the question.

Extract pattern substring from NSString

I've created my own UITabBarController.
Additionally I've written a few lines of code to determine the current user.
E.g. if I am the current user do/display this, otherwise do/display this etc...
The format pattern is (firstname Lastname).
The Full name of the current user is in "displayName".
This is how I set the title of the tab depending on whether I am looking at 'my' tabs or someone else's tabs.
[activities setTitle:[viewingUser objectForKey:#"displayName"]];
I now want to extract only the firstname and display it like so:
"firstname's".
I do know of substringToIndex and substringWithRange but I just can't seem to work it out myself. I reckon I just need to find the first and extract the part it togehter with that ['s]. Can anybody please point me in the right direction?
Cheers
If first name and last name are separated by a space then simply execute the following statement which returns an NSArray which in your will contain the first and last names.
[displayName componentsSeparatedByString:#" "]
Take a look at the NSScanner documentation and associated sample code. There are simpler ways to do it if that is your only dataset, however, the moment you start getting into even semi-complex sequences, you'll need other, more powerful solutions. This is why I'm recommending NSScanner off the top.

Regexp to parse out a person's name?

This might be a hard one (if not impossible), but can anyone think of a regular expression that will find a person's name, in say, a resume? I know this won't be 100% accurate, but I can't come up with something.
Let's assume the name only shows up once in the document.
No, you can't use regular expressions for this. The only chance you have is if the document is always in the same format and you can find the name based on the context surrounding it. But this probably isn't the case for you.
If you are asking your applicants to submit their résumé online you could provide a separate field for them to enter their name and any other information you need instead of trying to automatically parse résumés.
Forget it - seriously.
Or expect to get a lot of applications from a Mr C Vitae
In my experience, having written something very similar (but a very long time ago), about 95% of resumes have the person's name as the very first line. You could probably have a pretty loose regex checking for alpha, hyphens, periods, and assume that's the name.
Obviously there's no way to do this 100% accurately, as you said, but this would be close.
Unless you wanted to build an expression that contained every possible name, or-ed together, the expression you are referring to is not "Regular," with a capital R. A good guess might be to go looking for the largest-font words in the document. If they follow a pattern that looks like firstname-lastname, name-initial-name, etc., you could call it a good guess...
That's a really hairy problem to tackle. The regex has to match two words that could be someone's name. The problem with that is that some people, of Hispanic origin, for example, might have a name that's more than 2 words. Also, how would you define two words to match for a name? Would you use a database of common first and last name fields? That might work unless someone has an uncommon name.
I'm reminded of a story of a COBOL teacher in college told me about an individual of Asian origin who's name would break every rule the programmers defined for a bank's internal system. His first name was "O." just the letter O.
The only remotely dependable way to nail down the regex would be if you had something to set off your search with; maybe if a line of text in the resume began with "Name: " then you'd know where to start looking.
tl;dr: People's names and individual resumes are too heavily varied for a regular expression to pick apart.
You could do something like Amazon does for book overviews: SIPs. This would require some after-the-fact double checking by humans but you might find the person's name(s) in there.