LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-30-2019, 10:13 PM   #1
Zero4
Member
 
Registered: Jun 2007
Posts: 71

Rep: Reputation: 0
regex for phrase like'word-word-word'


Hi
I am trying to create a regex for a phrase like this
Code:
Silver-Surfer-Marvel.jpg
I need the regex to allow it, if there are 2 '-' in the pharse.
 
Old 06-30-2019, 10:20 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,142

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
You know what you want, so what have you tried ?.
 
1 members found this post helpful.
Old 07-01-2019, 09:41 PM   #3
Zero4
Member
 
Registered: Jun 2007
Posts: 71

Original Poster
Rep: Reputation: 0
Well I have tred all sorts of things, All of them on regexr.com. It is easy to match 1 "-"
But matching 2 a little harder. This is the closest I have come so far

/-\w{2}\

Others
/[A-Za-z]-[A-Za-z]-[A-Za-z]\
/\w-\w-\w*\
 
Old 07-01-2019, 10:14 PM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730
Which tool are you using to apply the expression? Perl or sed or something else? In other words, which type of regular expression do you wish to use?

The methods of writing character classes will vary. In perl and perl-compatible regular expressions an alphanumeric character can be written as \w or [[:alpha:]]. In POSIX, it is written as [[:alpha:]] Then there is also Extended POSIX.

In each style there are modifiers to match sequences. {1,} + * and so on. But not the same in all styles.

Last edited by Turbocapitalist; 07-01-2019 at 10:15 PM.
 
Old 07-01-2019, 10:49 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,142

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
As mentioned, \w is for a single character - to match a word, you need the "+" modifier which says "one or more". So your last attempt above is very close.
Note that \w is not strictly the same as [[:alpha:]] - if you use underscores for example. I do regularly in filenames in lieu of spaces.
 
Old 07-02-2019, 04:51 AM   #6
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,674

Rep: Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888
Quote:
Originally Posted by syg00 View Post
Note that \w is not strictly the same as [[:alpha:]] - if you use underscores for example. I do regularly in filenames in lieu of spaces.
And filenames quite often do have digits (0-9) too and/or other special characters, like i.e. single quotes. For example this Norah Jones song:
Quote:
Don't_Know_Why_(Live_In_NYC).mp3
which contains underscores, a singe quote and a pair of parentheses.
 
Old 07-02-2019, 04:56 AM   #7
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,674

Rep: Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888
Quote:
Originally Posted by Turbocapitalist View Post
an alphanumeric character can be written as \w or [[:alpha:]]. In POSIX, it is written as [[:alpha:]]
To be strict [[:alpha:]] is a LETTER (alphabetic character in the current locale), an alphanumeric one is [[:alnum:]]
\w is a synonym for [_[:alnum:]], so the class alnum extended with an underscore.
 
1 members found this post helpful.
Old 07-06-2019, 01:04 AM   #8
Zero4
Member
 
Registered: Jun 2007
Posts: 71

Original Poster
Rep: Reputation: 0
Thank you all
I want this to work in php: preg_match()

On reflection, I did not explain it very well

SO the expression "Silver-Surfer-Marvel.jpg"

If this is writen like this,
Surfer-Marvel.jpg
Silver-SurferMarvel.jpg

I want them to fail. It must have both "-"

I hope that help
 
Old 07-06-2019, 01:33 AM   #9
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,142

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
Given all the help above, you answer is .... ?
 
Old 07-06-2019, 06:36 AM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,807

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
word-word-word is \w+-\w+-\w+
(The \w stems from the perl RE and is in a recent glibc)
 
  


Reply

Tags
regex, regexp



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 01:10 AM
Perl to find regex and print following 5 lines after regex casperdaghost Linux - Newbie 3 08-29-2010 08:08 PM
Regex to exclude a specific phrase grob115 General 5 11-19-2008 12:10 PM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 05:10 AM
Need a regex, I suck at regex's d3funct Programming 4 02-25-2002 08:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration