473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Regex problem: finding part of string that *doesnt necessarily* have to be there

Markus
6,050 Expert 4TB
I was intruiged as to how to build a parse that would run like bbcode does.

So's i had a go at it, somewhat shakily and naively.

As was expected, i hit a snare: when searching for a [url=http...] i could easily turn this into a real url using
[php]
preg_replace('#\[url=(.*)\](.*)\[/url\]#', "<a href='$1'>$2</a>", $_string);
[/php]
The problem was hit when i wanted to give the user the option of an extra - the 'target' attribute. I managed to get it working using this:
[php]
preg_replace('#\[url=(.*) target=(.*)\](.*)\[/url\]#', "<a target='$2' href='$1'>$3</a>", $_string);
[/php]
but obviously if the target isn't present problems will arise.

So, my question is: how can i have extra(s*) in the url that dont have to be there?

And how would i go about adding others such as: title, name, id... etc.

Kind regards!

p.s. im terrible of thinking up thread titles! :(
Apr 16 '08 #1
2 1520
I was intruiged as to how to build a parse that would run like bbcode does.

So's i had a go at it, somewhat shakily and naively.

As was expected, i hit a snare: when searching for a [url=http...] i could easily turn this into a real url using
[php]
preg_replace('#\[url=(.*)\](.*)\[/url\]#', "<a href='$1'>$2</a>", $_string);
[/php]
The problem was hit when i wanted to give the user the option of an extra - the 'target' attribute. I managed to get it working using this:
[php]
preg_replace('#\[url=(.*) target=(.*)\](.*)\[/url\]#', "<a target='$2' href='$1'>$3</a>", $_string);
[/php]
but obviously if the target isn't present problems will arise.

So, my question is: how can i have extra(s*) in the url that dont have to be there?

And how would i go about adding others such as: title, name, id... etc.

Kind regards!

p.s. im terrible of thinking up thread titles! :(
It's simple, to make something optional, wrap it in parentheses and add a "?" at the end of it.
For example, let's say you wanted to search for "something is fantastic" but you want "is" to be optional:
Expand|Select|Wrap|Line Numbers
  1. /\bsomething( is)? fantastic\b/
The ? operator simple makes the last symbol optional, not the last word. A simple would be a set (something between parentheses) or a single character. So
Expand|Select|Wrap|Line Numbers
  1. is?
would match "i" or "is", but not "".

Hope that helped.
Visit http://regular-expressions.info to go through a complete walkthrough of Regexes.
May 7 '08 #2
Markus
6,050 Expert 4TB
It's simple, to make something optional, wrap it in parentheses and add a "?" at the end of it.
For example, let's say you wanted to search for "something is fantastic" but you want "is" to be optional:
Expand|Select|Wrap|Line Numbers
  1. /\bsomething( is)? fantastic\b/
The ? operator simple makes the last symbol optional, not the last word. A simple would be a set (something between parentheses) or a single character. So
Expand|Select|Wrap|Line Numbers
  1. is?
would match "i" or "is", but not "".

Hope that helped.
Visit http://regular-expressions.info to go through a complete walkthrough of Regexes.
Thanks alot helimeef, i'll have a go later - it's been a while since I posted this thread, so I'd kind of forgotten about it :P

Cheers
May 7 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
3
by: jg | last post by:
I made a mistake somewhere in my vb code and I look, check and read against the articles and help on regex, I still can't find the mistake I made. I know my test string and the test patterns...
8
by: Bob | last post by:
I need to create a Regex to extract all strings (including quotations) from a C# or C++ source file. After being unsuccessful myself, I found this sample on the internet: ...
17
by: Mark | last post by:
I must create a routine that finds tokens in small, arbitrary VB code snippets. For example, it might have to find all occurrences of {Formula} I was thinking that using regular expressions...
16
by: Mark Chambers | last post by:
Hi there, I'm seeking opinions on the use of regular expression searching. Is there general consensus on whether it's now a best practice to rely on this rather than rolling your own (string)...
10
by: bullockbefriending bard | last post by:
first, regex part: I am new to regexes and have come up with the following expression: ((1|),(1|)/){5}(1|),(1|) to exactly match strings which look like this: 1,2/3,4/5,6/7,8/9,10/11,12 ...
7
by: Nightcrawler | last post by:
Hi all, I am trying to use regular expressions to parse out mp3 titles into three different groups (artist, title and remix). I currently have three ways to name a mp3 file: Artist - Title ...
1
by: jonnyboy6969 | last post by:
Hi All Really hoping someone can help me out here with my deficient regex skills :) I have a function which takes a string of HTML and replaces a term (word or phrase) with a link. The pupose...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.