Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to match word pairs joined with colons

Tags:

regex

I don't know regular expression at all. Can anybody help me with one very simple regular expression which is,

extracting 'word:word' from a sentence. e.g "Java Tutorial Format:Pdf With Location:Tokyo Javascript"?

  • Little modification: the first 'word' is from a list but second is anything. "word1 in [ABC, FGR, HTY]"
  • guys situation demands a little more modification. The matching form can be "word11:word12 word13 .. " till the next "word21: ... " .

things are becoming complex with sec.....i have to learn reg ex :(

thanks in advance.

like image 253
Subrat Avatar asked Jan 23 '23 11:01

Subrat


2 Answers

You can use the regex:

\w+:\w+

Explanation:
\w - single char which is either a letter(uppercase or lowercase), digit or a _.
\w+ - one or more of above char..basically a word

so \w+:\w+ would match a pair of words separated by a colon.

like image 103
codaddict Avatar answered Mar 07 '23 20:03

codaddict


Try \b(\S+?):(\S+?)\b. Group 1 will capture "Format" and group 2, "Pdf".

A working example:

<html>
<head>
<script type="text/javascript">
function test() {
    var re = /\b(\S+?):(\S+?)\b/g; // without 'g' matches only the first
    var text = "Java Tutorial Format:Pdf With Location:Tokyo  Javascript";

    var match = null;
    while ( (match = re.exec(text)) != null) {
        alert(match[1] + " -- " + match[2]);
    }

}
</script>
</head>
<body onload="test();">

</body>
</html>

A good reference for regexes is https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/RegExp

like image 44
mcrisc Avatar answered Mar 07 '23 20:03

mcrisc