首页 \ 问答 \ 从正则表达式匹配中仅排除拖尾空格字符(Exclude only tailing space character from regex match)

从正则表达式匹配中仅排除拖尾空格字符(Exclude only tailing space character from regex match)

我试图从地址字符串中仅选择街道/单元号。

我的正则表达式是/^[\d\s,\-\/]*/

目前我的正则表达式在任何字母字符之前匹配digits '/' '-'space characters字符。 但是我想排除拖尾space character

我的正则表达式匹配

悉尼新南威尔士州悉尼路61/2号(注意'2'后的空间特征)

代替

新南威尔士州曼利61/2悉尼路61/2


以下是一些示例地址字符串:

61/2 Sydney Road, Manly NSW

61-2 Sydney Road, Manly NSW

61/2 3 Sydney Road, Manly NSW


I am trying to select only the the street / unit number from an address string.

My regex pattern is /^[\d\s,\-\/]*/

Currently my regex matches digits '/' '-' and space characters before any alphabetic characters. However I would like to exlude the tailing space character.

My regex matches

61/2Sydney Road, Manly NSW (note the space character after the '2')

Instead of

61/2 Sydney Road, Manly NSW


Here are some sample addresses strings:

61/2 Sydney Road, Manly NSW

61-2 Sydney Road, Manly NSW

61/2 3 Sydney Road, Manly NSW


原文:https://stackoverflow.com/questions/48738116
更新时间:2022-01-19 20:01

最满意答案

当你这样做:

char * str = "aaa bbb ccc ddd qqq";

字符串文字"aaa bbb ccc ddd qqq"放在只读数据部分中,并使str指向它。 在这一点上,改变str指向的是未定义的行为,并希望提出一个段错误。

如果您绝对想要使用指针,请尝试以下方法:

char * str = strdup("aaa bbb ccc ddd qqq");

这将在堆上创建一个可写的字符串文字的副本

一旦你完成了它,你需要free(str); 释放记忆。


When you do this:

char * str = "aaa bbb ccc ddd qqq";

The string literal "aaa bbb ccc ddd qqq" is placed in the read-only data section, and str is made to point to it. At this point changing what str points to is undefined behavior and will hopefully raise a segfault.

If you absolutely want to use a pointer, try this instead:

char * str = strdup("aaa bbb ccc ddd qqq");

This will make a copy of the string literal on the heap, which will be writable.

Once you're done with it though you'll need to free(str); to release the memory.

相关问答

更多
  • 根据标准,复制指针q = p; ,是未定义的行为。 阅读J.2未定义的行为状态: 使用了一个指向生命周期结束的对象的指针值(6.2.4)。 到那一章我们看到: 6.2.4对象的存储持续时间 对象的生命周期是程序执行的一部分,在此期间保证存储被保留。 一个对象存在,具有一个常量地址,33)并在其整个生命周期内保留其最后存储的值.34)如果一个对象在其生命周期之外被引用,行为是未定义的。 当指向(或刚刚过去)的对象达到其生命周期结束时,指针的值将变得不确定。 什么是不确定的 3.19.2 不确定值 :未指定值或 ...
  • 所以,char占用一个字节,是正确的? 是。 如果我理解正确,默认情况下char *占用一个字节的内存。 你的措辞有些含糊。 char占用一个字节的内存。 char *可以指向一个char ,即一个字节的内存,或一个字符数组 ,即多个字节的内存。 指针本身占用不止一个字节。 确切的值是实现定义的,通常是4个字节(32位)或8个字节(64位)。 你可以用printf( "%zd\n", sizeof char * )检查确切的值。 如果我们有一个char* str = "hello" ,那么假设它占用6个字节 ...
  • 当你这样做: char * str = "aaa bbb ccc ddd qqq"; 字符串文字"aaa bbb ccc ddd qqq"放在只读数据部分中,并使str指向它。 在这一点上,改变str指向的是未定义的行为,并希望提出一个段错误。 如果您绝对想要使用指针,请尝试以下方法: char * str = strdup("aaa bbb ccc ddd qqq"); 这将在堆上创建一个可写的字符串文字的副本 。 一旦你完成了它,你需要free(str); 释放记忆。 When you do thi ...
  • Sean是对的,two.nxt永远不会初始化。 实际上,num.nxt永远不会为num的任何实例初始化。 如果类更健壮,则不需要成员nxt。 可以使用nxt指针代替: class num { private: long i; num *nxtnum; public: num (long value) : i (value), nxtnum (0) { } void check () { if (number % i != 0) { ...
  • 编译器保留足够大的内存位置来存储文字并将其地址分配给指针。 此后,您可以像使用普通char *一样使用它char * 。 一个警告是,你不能修改它指向的内存。 char *str = "This is the end"; printf("%s\n", str); str[5] = 0; /* Illegal. */ 顺便说一句,这个C FAQ也讨论了这个问题。 The compiler reserves some memory location large enough to store the lit ...
  • 不,别担心! 你可以将指针想象成像int s或double s一样管理(至少在内存方面)。 指针本身就像是一个int ,它恰好包含了一些其他对象或对象数组的地址。 指针从作用域中消失后,指针本身的内存将自动恢复。 如果你正在做类似int** p = new int*[1]那样的例外,即用new创建指针。 那么你会在某个时候需要delete p 。 如果你正在创建你的指针,像int* p = new int[size]; (这可能是你想要的),那么p本身就在堆栈上 ,这意味着你不需要关心内存释放,但是数组p指 ...
  • 所以我的问题是,在CreateList函数中,程序如何为temp分配内存? 这不是,这是一个问题。 它应该做这样的事情: temp = malloc(sizeof(Listhead)); 并且代码* hallocp = temp将temp LIST复制到hallocbuf数组中? 它将保存在temp的指针复制到hallocbuf的第一个元素中(假设hallocp在任何地方都没有改变,仍然具有已经初始化的值,指向hallocbuf[0] )。 通常,隐藏LIST和Node是typedefs背后的指针这个事实 ...
  • 第一个有内存泄漏,我理解,因为x是一个指向B对象的A对象。 不,如果A和B具有明确的继承关系,则不存在内存泄漏。 如: class A { public: virtual ~A() {} }; class B : public A { }; 为什么第二个函数没有内存泄漏? 因为x分配在堆栈上,不需要也不能调用delete 。 x将被自动销毁。 我们不需要删除x? 只有new创建的对象需要delete 。 编辑 对于A和B代码,第一种情况会导致内存泄漏,因为A (基类)的析构函数不是虚拟的 。 对 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)