A regular expression for matching pairs of words connected to colons

I do not know the regular expression at all. Can someone help me with one very simple regular expression which,

extracting word words: word "from sentences. for example," Java Tutorial " Format: Pdf C Location: Tokyo Javascript"?

  • A slight modification: the first "word" refers to the list, but the second is anything. "word1 in [ABC, FGR, HTY]"
  • The situation with guys requires a bit more modification. The corresponding form can be the word "word11: word12 word13 .." to the next word "word21: ...".

everything becomes complicated with sec ..... I have to learn reg ex :(

early.

+3
source share
7 answers

You can use regex:

\w+:\w+

Explanation:
\w- a single char, which is either a letter (in upper or lower case), or a digit or _.
\w+- one or more of the above char .. basically a word

so \w+:\w+ will match a couple of words separated by a colon.

+5
source

Give it a try \b(\S+?):(\S+?)\b. Group 1 will take "Format" and group 2 "Pdf".

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>

Good link for regular expressions https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/RegExp

+2
source

:

 
$str=" this is pavun:kumar hello world bk:systesm" ;
if ( preg_match_all  ( '/(\w+\:\w+)/',$str ,$val ) )
 {
 print_r ( $val ) ;
 }
 else
 {
 print "Not matched \n";
 }
+1

Jaรบ :

function test() {
    var words = ['Format', 'Location', 'Size'],
            text = "Java Tutorial Format:Pdf With Location:Tokyo Language:Javascript", 
            match = null;
    var re = new RegExp( '(' + words.join('|') + '):(\\w+)', 'g');
    while ( (match = re.exec(text)) != null) {
        alert(match[1] + " = " + match[2]);
    }
}
+1

nodejs , , , :

([\w]+:)("(([^"])*)"|'(([^'])*)'|(([^\s])*))

. a:"b" c:'d e' f:g

es6:

const regex = /([\w]+:)("(([^"])*)"|'(([^'])*)'|(([^\s])*))/g;
const str = `category:"live casino" gsp:S1aik-UBnl aa:"b" c:'d e' f:g`;
let m;

while ((m = regex.exec(str)) !== null) {
   // This is necessary to avoid infinite loops with zero-width matches
   if (m.index === regex.lastIndex) {
      regex.lastIndex++;
   }

   // The result can be accessed through the `m`-variable.
   m.forEach((match, groupIndex) => {
      console.log(`Found match, group ${groupIndex}: ${match}`);
   });
}

PHP

$re = '/([\w]+:)("(([^"])*)"|\'(([^\'])*)\'|(([^\s])*))/';
$str = 'category:"live casino" gsp:S1aik-UBnl aa:"b" c:\'d e\' f:g';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
var_dump($matches);

/ -: https://regex101.com

Btw, regex101.com,

0

, , , , ":", , . , Python

>>> s="Java Tutorial Format:Pdf With Location:Tokyo Javascript"
>>> for i in s.split():
...     if ":" in i:
...         print i
...
Format:Pdf
Location:Tokyo

, , "someord: someord", ":" , . ,

>>> for i in s.split():
...     if ":" in i:
...         a=i.split(":")
...         if len(a) == 2:
...             print i
...
Format:Pdf
Location:Tokyo
-1
([^:]+):(.+)

: (, : ),:, ( )

... , ...

-2

Source: https://habr.com/ru/post/1735210/


All Articles