473,837 Members | 1,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regular Expression - "\" chars at the end of the string

I had a problem with string which have some \ chars at the end of the
string. For example:

$String = "I want this **** out of here\\\\\\\\\";

I needed to remove it useing regular expression...i thinked that
something like "/\\+$/" is enough but i was wrong. I made some tries
and finaly i removed "\" chars it with "/\\\+$/". It works (i'm proud
with myself :)), but i can't understand why \\\ matchs single \, and \\
not...

Jul 17 '05 #1
5 2074
sbd! wrote:
I had a problem with string which have some \ chars at the end of the
string. For example:

$String = "I want this **** out of here\\\\\\\\\";

I needed to remove it useing regular expression...i thinked that
something like "/\\+$/" is enough but i was wrong. I made some tries
and finaly i removed "\" chars it with "/\\\+$/". It works (i'm proud
with myself :)), but i can't understand why \\\ matchs single \, and \\
not...


Because (as far as I understand it) backslashes have to be escaped
twice: once for the regular expression engine, and once for PHP.

JP

--
Sorry, <de*****@cauce. org> is a spam trap.
Real e-mail address unavailable. 5000+ spams per month.
Jul 17 '05 #2
"Jan Pieter Kunst" <de*****@cauce. org> wrote in message
news:42******** *************** @news.xs4all.nl ...
: sbd! wrote:
: > I had a problem with string which have some \ chars at the end of the
: > string. For example:
: >
: > $String = "I want this **** out of here\\\\\\\\\";
: >
: > I needed to remove it useing regular expression...i thinked that
: > something like "/\\+$/" is enough but i was wrong. I made some tries
: > and finaly i removed "\" chars it with "/\\\+$/". It works (i'm proud
: > with myself :)), but i can't understand why \\\ matchs single \, and \\
: > not...
: >
:
: Because (as far as I understand it) backslashes have to be escaped
: twice: once for the regular expression engine, and once for PHP.

One way to get round this is to use single quotes instead:

'/\\+$/' gets round the esacaping problem. Don't double-quote PREG strings,
it gives you no end of trouble!

If you have to interpolate variable values into perl regexes, have a look at
http://php.net/preg_quote

Matt
Jul 17 '05 #3
Jan Pieter Kunst wrote:
: Because (as far as I understand it) backslashes have to be escaped
: twice: once for the regular expression engine, and once for PHP.
Matt Mitchell wrote:
One way to get round this is to use single quotes instead:

'/\\+$/' gets round the esacaping problem.


No, you still need to escape the backslash twice.

That pattern matches the same as "/\\+$/"; that is, a plus
sign at the end of the subject string or before a newline at
the end. Only one backslash is passed to the regular
expression engine, so the plus sign no longer carries its
special meaning as a quantifier.

http://www.php.net/manual/en/language.types.string.php

--
Jock
Jul 17 '05 #4
Thank you! All answers are helpful!

Jul 17 '05 #5
"John Dunlop" <us*********@jo hn.dunlop.name> wrote in message : >
: > '/\\+$/' gets round the esacaping problem.
:
: No, you still need to escape the backslash twice.
:

Whoops, not thinking before posting again...

<g>
Matt
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2220
by: Derek Basch | last post by:
Hello, I have a string like: string = "WHITE/CLARET/PINK/XL" which I need to alter to this format: string = "WHITE-CLARET-PINK/XL"
5
5990
by: KidLogik | last post by:
Hello! I am converting an infix expression string into a postfix so that I will be able to evaluate it easier -> (5*(((9+8)*(4*6))+7)) == 598+46**7+* I believe the rule is "Replace all occurances of two operands followed by an operator by their infix equivalent"
0
1286
by: Dave | last post by:
I am trying to return a regular expression from code behind to server control in aspx page. Problem is regularexpressionvalidator tag interprets string incorrectly. // aspx <asp:regularexpressionvalidator validationexpression="<%# getTimeExpr()%>" // code behind
1
1461
by: paulsmith5 | last post by:
Hi, I'm developing a web app and I want to validate a users input on a form. I need a regular expression to validate a string which must begin with a letter (i.e. A-Z or a-z) and must have 5 numbers (0-9) after it. For example, A12345. Hope someone can help. Thanks, Paul
3
3191
by: Rincevent | last post by:
Hi, I'm trying to process a large number of files, each with a very small size (about 500 chars maximum). I need to retrieve multiple information from each file, and some of these are optional. So to "simplify" the process, I undefed $/ (variable contains the full file). The current regular expression that I know works (with all mandatory matches) is the following: /\{2:+(\d{6})\d{4}.\}.*^:25:(.+?$).*^:13D:(\d{6})/sm as can be seen,...
56
6793
by: Adem | last post by:
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15) says under 6.4.2(2) : case constant-expression : I propose that the case expression of the switch statement be changed from "integral constant-expression" to "integral expression".
0
9828
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10863
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10611
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9390
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7801
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6988
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5663
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4034
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3120
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.