PCRE regex for string with delimiter - delimiter

I want to construct a PCRE regex for following kind of path string:
/root/product/db.main;/root/product/db.part
Please note that there are only two path strings separated by semicolon, that's it.
I was thinking of something like this: [ \t]*([\/\w ._-];+) but it doesn't seem to work.
Any suggestions?
Thanks in advance.

Try this regex pattern:
^(?:\/\w+)+(?:\.\w+)?(?:;(?:\/\w+)+(?:\.\w+)?)+$
This matches two or more paths, separated by semicolons. If you only want to match exactly two paths, then use this:
^(?:\/\w+)+(?:\.\w+)?(?:;(?:\/\w+)+(?:\.\w+)?)$
Demo

Related

Difficulty applying a Regex to a Rails View. Should I make it a helper method?

I am trying to apply the following regex to one of my views:
^([^\s]+)\s+
This is to remove any string of consecutive non-whitespace characters including any white space characters that follow from the start of the line (remove everything except the first word). I have input it on Rubular and it works.
I was wondering how I would be able to apply it to my rails project. Would I create a rails helper method? So far I have tested it in irb and it is not returning the right value:
I would like to know how I can fix my method and if making it a helper method is the right approach. Thank you very much for your help guys!
The =~ operator matches the regular expression against a string, and it returns either the offset of the match from the string if it is found, otherwise nil.
You could either try it with String.match and work with the match data.
like
str.match(^([^\s]+)\s+)
or you don't use regex for readability. Split the string on spaces and return and array of the words and take the first one, like:
str.split(' ').first

How to concatenate strings in Sightly/HTL?

I have the following code:
<sly data-sly-use.link="${'core.impl.view.tools.LinkUtils' # path=properties.targetURL}"></sly>
I want to concatenate properties.linkType to properties.targetURL.
Any ideas how it can be done? I've found examples on the net but they don't seem to work for my situation.
That depends on what kind of string concatenation you have in mind:
Concatenating strings using an operator is not supported, ie. you cannot do ${properties.targetURL + properties.linkType}. A workaround (suggested by #Jens) is to do something like: <sly data-sly-test.concatenated="${'{0}{1}' # format=[properties.targetURL, properties.linkType]}"></sly>
Concatenating strings in HTML output can be done by placing HTL expression next to each other, ie. ${properties.targetUrl}${properties.linkType}
Sending both strings to an Use Object is supported via multiple expression options: <sly data-sly-use.link="${'core.impl.view.tools.LinkUtils' # path=properties.targetURL, type=properties.linkType}"></sly>
Concatenating strings to form an URL might be possible in some cases using URI Manipulation
I just want to add one more way to concatenate strings to the above answer, by using # join.
<sly data-sly-test="${['String1','String2','String3'] # join = '-'}"/>
It will give output as:
String1-String2-String3

Regular expression string between tags "<string>"

I have a string "<wpf><xaml><wpf-controls>".
I need the string between the tags in array format.
How do I get this?
The regex for this problem is really simple it is: /<(.*?)>/
For the array part is would reference to the answer on how to use one line regular expression to get matched content
EDIT:
for array of the insides of the tags use <wpf><xaml><wpf-controls>".scan(/(?:<(.*?)>)*/)
The (?: .. ) groups the tag together and the * says we want 0 or more of that group :)
'<wpf><xaml><wpf-controls>'.scan(/<(.*?)>/).map(&:first)

ruby regex is using last match to seperate string but should use first

Im parsing the source of a website and Im using this regex:
/page\.php\?id\=([0-9]*)\"\>(.*)\<\/a\>\<\/span\>/.match(self.agent.page.content)
self.agent.page.content contains the source of the page fetched by mechanize. The regex basicly works but in the secound match it does fetch more then it should because there are more then one <\/a\>\<\/span\> in the source and the regex uses the last one so I get a bunch of html crap. How can I tell the regex to use the first match as an "end marker"?
.* is greedy, whereas .*? is non-greedy. Try:
/page\.php\?id\=([0-9]*)\"\>(.*?)\<\/a\>\<\/span\>/.match(self.agent.page.content)

Rails validates_format_of

I want to use validates_format_of to validate a comma separated string with only letters (small and caps), and numbers.
So.
example1, example2, 22example44, ex24
not:
^&*, <> , asfasfsdafas<#%$#
Basically I want to have users enter comma separated words(incl numbers) without special characters.
I'll use it to validate tags from acts_as_taggable_on. (i don't want to be a valid tag for example.
Thanks in advance.
You can always test out regular expressions at rubular, you would find that both tiftiks and Tims regular expressions work albeit with some strange edge cases with whitespace.
Tim's solution can be extended to include leading and trailing whitespace and that should then do what you want as follows :-
^\s*[A-Za-z0-9]+(\s*,\s*[A-Za-z0-9]+)*\s*$
Presumably when you have validated the input string you will want to turn it into an array of tags to iterate over. You can do this as follows :-
array_var = string_var.delete(' ').split(',')
^([a-zA-Z0-9]+,\s*)*[a-zA-Z0-9]+$
Note that this regex doesn't match values with whitespace, so it won't match multiple words like "abc xyz, fgh qwe". It matches any amount of whitespace after commas. You might not need ^ or $ if validates_format_of tries to match the whole string, I've never used Rails so I don't know about that.
^[A-Za-z0-9]+([ \t]*,[ \t]*[A-Za-z0-9]+)*$
should match a CSV line that only contains those characters, whether it's just one value or many.

Resources