Home > Mobile >  Regex pattern match how to leave one with spaces in it?
Regex pattern match how to leave one with spaces in it?

Time:09-09

I have the following example string : <ia:save-config xmlns:ia= "http://google.com/yang/ia" />

Basically, I need a regex pattern that matches a string starting with '<' and ending with ':' but with has no space in between. For eg in the above string I want to write a regex pattern so that it matches '<ia:' but not '<rpc xmlns= "urn:' since the second one has spaces in between.

I have the following regex pattern till now: '<.*?:' but it matches both above mentioned text..

CodePudding user response:

The . matches everything except linebreaks, including < and :

You can use this:

<\w ?:

which matches alphanumeric characters (letters, numbers, and underscores) between the < and :.

CodePudding user response:

To match a substring between two different chars that contains no whitespace, you can use

<[^<:\s]*:

See the regex demo.

Details:

  • < - a < char
  • [^<:\s]* - zero or more chars other than <, : and whitespace (\s)
  • : - a colon.
  • Related