Helpful Information
 
 
Category: Regex Programming
Problem with expression

I need to find:

"{SomeString ...anything goes here... }"

Where the streing starts out with "{SomeString" and ends with "}", but can have anything in between.

I would be using preg_replace...

I'm not getting anywhere.

Anything... except a } of course. Remember that.

{ and } are special characters: escape them with a backslash. Use [] for a set, then negate it, and include whatever character you don't want. A * means "any number of" - use that on the character set.

...

{ and } are special characters...

... in some cases.
See: http://www.regular-expressions.info/characters.html#special

How about this:

$pattern = '/\{SomeString\s*[0-9a-fA-F\s]\}/';to match "Opening Curly Brace + SomeString + Space Char + Any Number of Letters and Numbers and Space Chars + Closing Curly Brace"

How about this:

$pattern = '/\{SomeString\s*[0-9a-fA-F\s]\}/';to match "Opening Curly Brace + SomeString + Space Char + Any Number of Letters and Numbers and Space Chars + Closing Curly Brace"

Try it!

Try it!I'm not sitting at a machine that has development software installed. Any friendly knowledgeable folks?

I'm not sitting at a machine that has development software installed. Any friendly knowledgeable folks?

What good would it do you if someone here would say "yes that is correct". You will still need to wait when you you get to a system that has the right software to test it yourself. Right?
Also, when performing an extensive test yourself, you might run into a corner case that you didn't mention here. What if someone here says: "yes, that is the correct regex" and you don't test it properly and therefore don't run into this corner case?

But yeah, what you said in your post, is more or less correct. I said "more or less" because \s matches not only single white spaces, but also new lines, tabs, etc. But again: test it yourself!

Note that you can also test your regex-es online. Just Google for "on line regex tester". This one is nice:
http://regex.larsolavtorvik.com/

Good luck!

I'm not sitting at a machine that has development software installed. Any friendly knowledgeable folks?

B.t.w, are you insinuating something with the cursive word "friendly"?

B.t.w, are you insinuating something with the cursive word "friendly"?Yes I am.

If you're unwilling to be helpful, why waste your time? Your attitude is very unfriendly.

I made an effort to work it out and asked if it looked good. If you didn't care to waste your time, you should have skipped commenting at all.

This is *NOT* some IRC channel.

And, EVEN if it does work, maybe there is a BETTER way to do what I want?

But clearly you are NOT a helpful person.

Yes I am.

If you're unwilling to be helpful, why waste your time? Your attitude is very unfriendly.

I made an effort to work it out and asked if it looked good. If you didn't care to waste your time, you should have skipped commenting at all.

This is *NOT* some IRC channel.

And, EVEN if it does work, maybe there is a BETTER way to do what I want?

But clearly you are NOT a helpful person.

So by telling you to try something, I am unfriendly? What an ungrateful piece of work you are!
I wasn't unfriendly to you. I didn't know you had no machine at hand where you could test it. And after you explained why you couldn't test it, didn't I gave you a proper explanation and didn't I explain why I suggested you to test it yourself?

I won't waste my time on you anymore!

Guys, knock it off. You've both insulted each other thoroughly so call it even.

Guys, knock it off. You've both insulted each other thoroughly so call it even.

Ah, I see, to make a point, you have to increase the size of your font. Good to know.

Granted, my last remark was not necessary (I removed it), but I still find the OP and ungrateful piece of work for calling me unhelpful

I also find it disappointing that people don't seek the discussion but rather "vote" me down using this forum's voting system. The easy way out, if you ask me.
A shame.

Anything... except a } of course. Remember that.

{ and } are special characters: ...

I was mild earlier. The above statement is false: '{' and '}' are not special characters. In some cases '{' is a special character but '}' is never one.

I was mild earlier. The above statement is false: '{' and '}' are not special characters. In some cases '{' is a special character but '}' is never one.Well, I've played with it a bit and think I was on the wrong track. From what I can tell...

What I had was not 'any number of letters and numbers': it only hexadecimal digits (0-F). \s doesn't just match spaces: it matches any whitespace character, including tabs, vtabs, &c., and I think you've applied the * wrongly: modifiers go after the terms they modify.

So I'm going to try this one:
'/\{SomeString [0-9a-zA-Z ]*\}/'

Well, I've played with it a bit and think I was on the wrong track. From what I can tell...

What I had was not 'any number of letters and numbers': it only hexadecimal digits (0-F). \s doesn't just match spaces: it matches any whitespace character, including tabs, vtabs, &c., and I think you've applied the * wrongly: modifiers go after the terms they modify.

So I'm going to try this one:
'/\{SomeString [0-9a-zA-Z ]*\}/'

Err, who is "you" in "and I think you've applied the * wrongly"?

Anyway, as I already mentioned: you don't need to escape the { and } in your case.

Run this example:


$text = '{abc def ghi}';
if(preg_match('/^{[a-z ]*}$/', $text)) {
echo "YES";
} else {
echo "NO";
}

It will print "YES": no need escaping them.

EDIT:

Ah, "you" was me. Yes, I misread your explanation. Well, what'd you know, I make my own point: no matter what people tell you on an online forum, always test it yourself.
; )










privacy (GDPR)